test_format_api.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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 Tests for the ast_event API
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. *
  24. * \ingroup tests
  25. *
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/module.h"
  34. #include "asterisk/test.h"
  35. #include "asterisk/format.h"
  36. #include "asterisk/format_cap.h"
  37. #include "asterisk/strings.h"
  38. /*! These are the keys for accessing attributes */
  39. enum test_attr_keys {
  40. TEST_ATTR_KEY_SAMP_RATE,
  41. TEST_ATTR_KEY_STRING,
  42. };
  43. /*! These are the values for the TEST_ATTR_KEY_SAMP_RATE key */
  44. enum test_attr_vals_samp {
  45. TEST_ATTR_VAL_SAMP_8KHZ = (1 << 0),
  46. TEST_ATTR_VAL_SAMP_12KHZ = (1 << 1),
  47. TEST_ATTR_VAL_SAMP_16KHZ = (1 << 2),
  48. TEST_ATTR_VAL_SAMP_32KHZ = (1 << 3),
  49. TEST_ATTR_VAL_SAMP_48KHZ = (1 << 4),
  50. };
  51. /*! This is the attribute structure used for our test interface. */
  52. struct test_attr {
  53. enum test_attr_vals_samp samp_flags;
  54. char string[32];
  55. };
  56. static enum ast_format_cmp_res test_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
  57. {
  58. struct test_attr *attr1 = (struct test_attr *) fattr1;
  59. struct test_attr *attr2 = (struct test_attr *) fattr2;
  60. if ((attr1->samp_flags == attr2->samp_flags) &&
  61. !(strcmp(attr1->string, attr2->string))) {
  62. return AST_FORMAT_CMP_EQUAL;
  63. }
  64. if ((attr1->samp_flags != (attr1->samp_flags & attr2->samp_flags)) ||
  65. (!ast_strlen_zero(attr1->string) && strcmp(attr1->string, attr2->string))) {
  66. return AST_FORMAT_CMP_NOT_EQUAL;
  67. }
  68. return AST_FORMAT_CMP_SUBSET;
  69. }
  70. static int test_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
  71. {
  72. struct test_attr *attr1 = (struct test_attr *) fattr1;
  73. struct test_attr *attr2 = (struct test_attr *) fattr2;
  74. struct test_attr *attr_res = (struct test_attr *) result;
  75. int joint = -1;
  76. attr_res->samp_flags = (attr1->samp_flags & attr2->samp_flags);
  77. if (attr_res->samp_flags) {
  78. joint = 0;
  79. }
  80. if (!strcmp(attr1->string, attr2->string)) {
  81. ast_copy_string(attr_res->string, attr1->string, sizeof(attr_res->string));
  82. joint = 0;
  83. }
  84. return joint;
  85. }
  86. static void test_set(struct ast_format_attr *fattr, va_list ap)
  87. {
  88. enum test_attr_keys key;
  89. struct test_attr *attr = (struct test_attr *) fattr;
  90. char *string;
  91. for (key = va_arg(ap, int);
  92. key != AST_FORMAT_ATTR_END;
  93. key = va_arg(ap, int))
  94. {
  95. switch (key) {
  96. case TEST_ATTR_KEY_SAMP_RATE:
  97. attr->samp_flags = (va_arg(ap, int) | attr->samp_flags);
  98. break;
  99. case TEST_ATTR_KEY_STRING:
  100. string = va_arg(ap, char *);
  101. if (!ast_strlen_zero(string)) {
  102. ast_copy_string(attr->string, string, sizeof(attr->string));
  103. }
  104. break;
  105. default:
  106. ast_log(LOG_WARNING, "unknown attribute type %u\n", key);
  107. }
  108. }
  109. }
  110. /*! uLaw does not actually have any attributes associated with it.
  111. * This is just for the purpose of testing. We are guaranteed there
  112. * will never exist a interface for uLaw already. */
  113. static struct ast_format_attr_interface test_interface = {
  114. .id = AST_FORMAT_TESTLAW,
  115. .format_attr_cmp = test_cmp,
  116. .format_attr_get_joint = test_getjoint,
  117. .format_attr_set = test_set
  118. };
  119. /*!
  120. * \internal
  121. */
  122. AST_TEST_DEFINE(format_test1)
  123. {
  124. struct ast_format format1 = { 0, };
  125. struct ast_format format2 = { 0, };
  126. struct ast_format joint = { 0, };
  127. switch (cmd) {
  128. case TEST_INIT:
  129. info->name = "ast_format_test1";
  130. info->category = "/main/format/";
  131. info->summary = "Test ast_format with attributes.";
  132. info->description =
  133. "This test exercises the Ast Format API by creating and registering "
  134. "a custom ast_format_attr_interface and performing various function "
  135. "calls on ast_formats using the interface. ";
  136. return AST_TEST_NOT_RUN;
  137. case TEST_EXECUTE:
  138. break;
  139. }
  140. if (ast_format_attr_reg_interface(&test_interface)) {
  141. ast_test_status_update(test, "test_interface failed to register.\n");
  142. return AST_TEST_FAIL;
  143. }
  144. /* set a format with a single attribute. */
  145. ast_format_set(&format1, AST_FORMAT_TESTLAW, 1,
  146. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  147. AST_FORMAT_ATTR_END);
  148. if (ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
  149. ast_test_status_update(test, "format1 did not set number attribute correctly.\n");
  150. return AST_TEST_FAIL;
  151. }
  152. if (!ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, AST_FORMAT_ATTR_END)) {
  153. ast_test_status_update(test, "format1 did not determine isset on number correctly. \n");
  154. return AST_TEST_FAIL;
  155. }
  156. /* append the string attribute to a format with previous attributes already set */
  157. ast_format_append(&format1,
  158. TEST_ATTR_KEY_STRING,"String",
  159. AST_FORMAT_ATTR_END);
  160. if (ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "String", AST_FORMAT_ATTR_END)) {
  161. ast_test_status_update(test, "format1 did not set string attribute correctly.\n");
  162. return AST_TEST_FAIL;
  163. }
  164. if (!ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "Not a string", AST_FORMAT_ATTR_END)) {
  165. ast_test_status_update(test, "format1 did not determine isset on string correctly. \n");
  166. return AST_TEST_FAIL;
  167. }
  168. /* set format2 with both STRING and NUMBER at the same time */
  169. ast_format_set(&format2, AST_FORMAT_TESTLAW, 1,
  170. TEST_ATTR_KEY_STRING, "MOOOoo",
  171. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  172. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  173. AST_FORMAT_ATTR_END);
  174. /* perform isset with multiple key value pairs. */
  175. if (ast_format_isset(&format2,
  176. TEST_ATTR_KEY_STRING, "MOOOoo",
  177. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  178. AST_FORMAT_ATTR_END)) {
  179. ast_test_status_update(test, "format2 did not set attributes correctly.\n");
  180. return AST_TEST_FAIL;
  181. }
  182. if (!ast_format_isset(&format2,
  183. TEST_ATTR_KEY_STRING, "WRONG",
  184. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  185. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  186. AST_FORMAT_ATTR_END)) {
  187. ast_test_status_update(test, "format2 did not deterine isset correctly.\n");
  188. return AST_TEST_FAIL;
  189. }
  190. /* get joint attributes between format1 and format2. */
  191. if (ast_format_joint(&format1, &format2, &joint)) {
  192. ast_test_status_update(test, "failed to get joint attributes.\n");
  193. return AST_TEST_FAIL;
  194. }
  195. if (ast_format_isset(&joint, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) {
  196. ast_test_status_update(test, "joint attribute was not what we expected.\n");
  197. return AST_TEST_FAIL;
  198. }
  199. /* exercise compare functions */
  200. if (ast_format_cmp(&format1, &format2) != AST_FORMAT_CMP_NOT_EQUAL) {
  201. ast_test_status_update(test, "cmp 1 failed.\n");
  202. return AST_TEST_FAIL;
  203. }
  204. if (ast_format_cmp(&format1, &format1) != AST_FORMAT_CMP_EQUAL) {
  205. ast_test_status_update(test, "cmp 2 failed.\n");
  206. return AST_TEST_FAIL;
  207. }
  208. if (ast_format_cmp(&joint, &format1) != AST_FORMAT_CMP_SUBSET) {
  209. ast_test_status_update(test, "cmp 3 failed.\n");
  210. return AST_TEST_FAIL;
  211. }
  212. /* unregister interface */
  213. if (ast_format_attr_unreg_interface(&test_interface)) {
  214. ast_test_status_update(test, "test_interface failed to unregister.\n");
  215. return AST_TEST_FAIL;
  216. }
  217. return AST_TEST_PASS;
  218. }
  219. /*!
  220. * \internal
  221. */
  222. AST_TEST_DEFINE(format_test2)
  223. {
  224. struct ast_format format = { 0, };
  225. switch (cmd) {
  226. case TEST_INIT:
  227. info->name = "ast_format_test2";
  228. info->category = "/main/format/";
  229. info->summary = "Test ast_format unique id and category system";
  230. info->description =
  231. "This test exercises the Ast Format unique id and category "
  232. "system by creating formats of various types and verifying "
  233. "their category matches what we expect.";
  234. return AST_TEST_NOT_RUN;
  235. case TEST_EXECUTE:
  236. break;
  237. }
  238. ast_format_set(&format, AST_FORMAT_ULAW, 0);
  239. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_AUDIO) {
  240. ast_test_status_update(test, "audio type failed\n");
  241. return AST_TEST_FAIL;
  242. }
  243. ast_format_set(&format, AST_FORMAT_H264, 0);
  244. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_VIDEO) {
  245. ast_test_status_update(test, "video type failed\n");
  246. return AST_TEST_FAIL;
  247. }
  248. ast_format_set(&format, AST_FORMAT_JPEG, 0);
  249. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_IMAGE) {
  250. ast_test_status_update(test, "image type failed\n");
  251. return AST_TEST_FAIL;
  252. }
  253. ast_format_set(&format, AST_FORMAT_T140, 0);
  254. if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_TEXT) {
  255. ast_test_status_update(test, "text type failed\n");
  256. return AST_TEST_FAIL;
  257. }
  258. return AST_TEST_PASS;
  259. }
  260. static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test)
  261. {
  262. int res = AST_TEST_PASS;
  263. struct ast_format_cap *cap_joint = NULL;
  264. struct ast_format tmpformat;
  265. if (ast_format_attr_reg_interface(&test_interface)) {
  266. ast_test_status_update(test, "test_interface failed to register.\n");
  267. ast_format_cap_destroy(cap1);
  268. ast_format_cap_destroy(cap2);
  269. return AST_TEST_FAIL;
  270. }
  271. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  272. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  273. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
  274. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0));
  275. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0));
  276. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0));
  277. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
  278. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0));
  279. ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  280. TEST_ATTR_KEY_STRING, "testing caps hooray",
  281. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  282. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  283. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
  284. AST_FORMAT_ATTR_END));
  285. /* Test is compatible */
  286. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
  287. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  288. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  289. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
  290. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
  291. !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  292. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n");
  293. res = AST_TEST_FAIL;
  294. goto test3_cleanup;
  295. }
  296. /* Test things that are not compatible */
  297. if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) ||
  298. ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) ||
  299. ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) {
  300. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n");
  301. res = AST_TEST_FAIL;
  302. goto test3_cleanup;
  303. }
  304. /* Test compatiblity with format with attributes. */
  305. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  306. TEST_ATTR_KEY_STRING, "testing caps hooray",
  307. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  308. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  309. AST_FORMAT_ATTR_END))) {
  310. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n");
  311. res = AST_TEST_FAIL;
  312. goto test3_cleanup;
  313. }
  314. if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  315. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  316. AST_FORMAT_ATTR_END))) {
  317. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n");
  318. res = AST_TEST_FAIL;
  319. goto test3_cleanup;
  320. }
  321. if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  322. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  323. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */
  324. AST_FORMAT_ATTR_END))) {
  325. ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n");
  326. res = AST_TEST_FAIL;
  327. goto test3_cleanup;
  328. }
  329. /* Lets start testing the functions that compare ast_format_cap objects.
  330. * Genreate the cap2 object to contain some similar formats as cap1
  331. * and some different formats as well. */
  332. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  333. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  334. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0));
  335. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0));
  336. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0));
  337. ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  338. TEST_ATTR_KEY_STRING, "testing caps hooray",
  339. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  340. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ,
  341. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  342. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ,
  343. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
  344. AST_FORMAT_ATTR_END));
  345. /* find joint formats between cap1 and cap2 */
  346. cap_joint = ast_format_cap_joint(cap1, cap2);
  347. if (!cap_joint) {
  348. ast_test_status_update(test, "failed to create joint capabilities correctly.\n");
  349. res = AST_TEST_FAIL;
  350. goto test3_cleanup;
  351. }
  352. /* determine if cap_joint is what we think it should be */
  353. if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  354. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  355. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) ||
  356. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  357. TEST_ATTR_KEY_STRING, "testing caps hooray",
  358. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  359. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  360. AST_FORMAT_ATTR_END))) {
  361. ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
  362. res = AST_TEST_FAIL;
  363. goto test3_cleanup;
  364. }
  365. /* make sure joint cap does not have formats that should not be there */
  366. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) ||
  367. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1,
  368. TEST_ATTR_KEY_STRING, "testing caps hooray",
  369. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ,
  370. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ,
  371. TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ,
  372. AST_FORMAT_ATTR_END))) {
  373. ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n");
  374. res = AST_TEST_FAIL;
  375. goto test3_cleanup;
  376. }
  377. /* Lets test removing a capability */
  378. if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  379. ast_test_status_update(test, "ast_format_cap_remove failed. \n");
  380. res = AST_TEST_FAIL;
  381. goto test3_cleanup;
  382. }
  383. /* Lets make sure what we just removed does not still exist */
  384. if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
  385. ast_test_status_update(test, "ast_format_cap_remove failed 2. \n");
  386. res = AST_TEST_FAIL;
  387. goto test3_cleanup;
  388. }
  389. /* Lets test removing a capability by id.*/
  390. if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) {
  391. ast_test_status_update(test, "ast_format_cap_remove failed 3. \n");
  392. res = AST_TEST_FAIL;
  393. goto test3_cleanup;
  394. }
  395. /* Lets make sure what we just removed does not still exist */
  396. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) {
  397. ast_test_status_update(test, "ast_format_cap_remove failed 4. \n");
  398. res = AST_TEST_FAIL;
  399. goto test3_cleanup;
  400. }
  401. /* lets test getting joint formats by type */
  402. ast_format_cap_destroy(cap_joint);
  403. if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) {
  404. ast_test_status_update(test, "ast_format_cap_get_type failed.\n");
  405. res = AST_TEST_FAIL;
  406. goto test3_cleanup;
  407. }
  408. /* lets make sure our joint capability structure has what we expect */
  409. if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) ||
  410. !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) {
  411. ast_test_status_update(test, "get_type failed 2.\n");
  412. res = AST_TEST_FAIL;
  413. goto test3_cleanup;
  414. }
  415. /* now make sure joint does not have anything but video */
  416. if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) ||
  417. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) ||
  418. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) ||
  419. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) ||
  420. ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) {
  421. ast_test_status_update(test, "get_type failed 3.\n");
  422. res = AST_TEST_FAIL;
  423. goto test3_cleanup;
  424. }
  425. /* now lets remove everythign from cap_joint */
  426. ast_format_cap_remove_all(cap_joint);
  427. if (!ast_format_cap_is_empty(cap_joint)) {
  428. ast_test_status_update(test, "failed to remove all\n");
  429. res = AST_TEST_FAIL;
  430. goto test3_cleanup;
  431. }
  432. /* now lets add all by type */
  433. ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO);
  434. if (ast_format_cap_is_empty(cap_joint)) {
  435. ast_test_status_update(test, "failed to add all by type AUDIO\n");
  436. res = AST_TEST_FAIL;
  437. }
  438. ast_format_cap_iter_start(cap_joint);
  439. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  440. if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) {
  441. ast_test_status_update(test, "failed to add all by type AUDIO\n");
  442. res = AST_TEST_FAIL;
  443. ast_format_cap_iter_end(cap_joint);
  444. goto test3_cleanup;
  445. }
  446. }
  447. ast_format_cap_iter_end(cap_joint);
  448. /* test append */
  449. ast_format_cap_append(cap_joint, cap1);
  450. ast_format_cap_iter_start(cap1);
  451. while (!(ast_format_cap_iter_next(cap1, &tmpformat))) {
  452. if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) {
  453. ast_test_status_update(test, "failed to append format capabilities.\n");
  454. res = AST_TEST_FAIL;
  455. ast_format_cap_iter_end(cap1);
  456. goto test3_cleanup;
  457. }
  458. }
  459. ast_format_cap_iter_end(cap1);
  460. /* test copy */
  461. cap1 = ast_format_cap_destroy(cap1);
  462. cap1 = ast_format_cap_dup(cap_joint);
  463. if (!ast_format_cap_identical(cap_joint, cap1)) {
  464. ast_test_status_update(test, "failed to copy capabilities\n");
  465. res = AST_TEST_FAIL;
  466. goto test3_cleanup;
  467. }
  468. /* test remove by type */
  469. ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO);
  470. if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) {
  471. ast_test_status_update(test, "failed to remove all by type audio\n");
  472. res = AST_TEST_FAIL;
  473. goto test3_cleanup;
  474. }
  475. if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */
  476. ast_test_status_update(test, "failed to remove all by type audio\n");
  477. res = AST_TEST_FAIL;
  478. goto test3_cleanup;
  479. }
  480. ast_format_cap_iter_start(cap_joint);
  481. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  482. if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) {
  483. ast_test_status_update(test, "failed to remove all by type audio\n");
  484. res = AST_TEST_FAIL;
  485. ast_format_cap_iter_end(cap_joint);
  486. goto test3_cleanup;
  487. }
  488. }
  489. ast_format_cap_iter_end(cap_joint);
  490. /* test add all */
  491. ast_format_cap_remove_all(cap_joint);
  492. ast_format_cap_add_all(cap_joint);
  493. {
  494. int video = 0, audio = 0, text = 0, image = 0;
  495. ast_format_cap_iter_start(cap_joint);
  496. while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) {
  497. switch (AST_FORMAT_GET_TYPE(tmpformat.id)) {
  498. case AST_FORMAT_TYPE_AUDIO:
  499. audio++;
  500. break;
  501. case AST_FORMAT_TYPE_VIDEO:
  502. video++;
  503. break;
  504. case AST_FORMAT_TYPE_TEXT:
  505. text++;
  506. break;
  507. case AST_FORMAT_TYPE_IMAGE:
  508. image++;
  509. break;
  510. }
  511. }
  512. ast_format_cap_iter_end(cap_joint);
  513. if (!video || !audio || !text || !image) {
  514. ast_test_status_update(test, "failed to add all\n");
  515. res = AST_TEST_FAIL;
  516. goto test3_cleanup;
  517. }
  518. }
  519. /* test copy2 */
  520. ast_format_cap_copy(cap2, cap_joint);
  521. if (!ast_format_cap_identical(cap2, cap_joint)) {
  522. ast_test_status_update(test, "ast_format_cap_copy failed\n");
  523. res = AST_TEST_FAIL;
  524. goto test3_cleanup;
  525. }
  526. test3_cleanup:
  527. ast_format_cap_destroy(cap1);
  528. ast_format_cap_destroy(cap2);
  529. ast_format_cap_destroy(cap_joint);
  530. /* unregister interface */
  531. if (ast_format_attr_unreg_interface(&test_interface)) {
  532. ast_test_status_update(test, "test_interface failed to unregister.\n");
  533. res = AST_TEST_FAIL;
  534. }
  535. return res;
  536. }
  537. /*!
  538. * \internal
  539. */
  540. AST_TEST_DEFINE(container_test1_nolock)
  541. {
  542. struct ast_format_cap *cap1;
  543. struct ast_format_cap *cap2;
  544. switch (cmd) {
  545. case TEST_INIT:
  546. info->name = "container_test_1_no_locking";
  547. info->category = "/main/format/";
  548. info->summary = "Test ast_format and ast_format_cap structures, no locking";
  549. info->description =
  550. "This test exercises the Ast Format Capability API by creating "
  551. "capability structures and performing various API calls on them.";
  552. return AST_TEST_NOT_RUN;
  553. case TEST_EXECUTE:
  554. break;
  555. }
  556. cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  557. cap2 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  558. if (!cap1 || !cap2) {
  559. ast_test_status_update(test, "cap alloc failed.\n");
  560. return AST_TEST_FAIL;
  561. }
  562. return container_test1_helper(cap1, cap2, test);
  563. }
  564. /*!
  565. * \internal
  566. */
  567. AST_TEST_DEFINE(container_test1_withlock)
  568. {
  569. struct ast_format_cap *cap1;
  570. struct ast_format_cap *cap2;
  571. switch (cmd) {
  572. case TEST_INIT:
  573. info->name = "container_test1_with_locking";
  574. info->category = "/main/format/";
  575. info->summary = "Test ast_format and ast_format_cap structures, with locking";
  576. info->description =
  577. "This test exercises the Ast Format Capability API by creating "
  578. "capability structures and performing various API calls on them.";
  579. return AST_TEST_NOT_RUN;
  580. case TEST_EXECUTE:
  581. break;
  582. }
  583. cap1 = ast_format_cap_alloc(0);
  584. cap2 = ast_format_cap_alloc(0);
  585. if (!cap1 || !cap2) {
  586. ast_test_status_update(test, "cap alloc failed.\n");
  587. return AST_TEST_FAIL;
  588. }
  589. return container_test1_helper(cap1, cap2, test);
  590. }
  591. static int container_test2_no_locking_helper(struct ast_format_cap *cap, struct ast_test *test)
  592. {
  593. int num = 0;
  594. struct ast_format tmpformat = { 0, };
  595. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0));
  596. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0));
  597. ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_G722, 0));
  598. ast_format_cap_iter_start(cap);
  599. while (!ast_format_cap_iter_next(cap, &tmpformat)) {
  600. num++;
  601. }
  602. ast_format_cap_iter_end(cap);
  603. ast_format_cap_iter_start(cap);
  604. while (!ast_format_cap_iter_next(cap, &tmpformat)) {
  605. num++;
  606. }
  607. ast_format_cap_iter_end(cap);
  608. ast_format_cap_destroy(cap);
  609. ast_test_status_update(test, "%d items iterated over\n", num);
  610. return (num == 6) ? AST_TEST_PASS : AST_TEST_FAIL;
  611. }
  612. /*!
  613. * \internal
  614. */
  615. AST_TEST_DEFINE(container_test2_no_locking)
  616. {
  617. struct ast_format_cap *cap;
  618. switch (cmd) {
  619. case TEST_INIT:
  620. info->name = "container_test2_no_locking";
  621. info->category = "/main/format/";
  622. info->summary = "Test ast_format_cap iterator, no locking";
  623. info->description =
  624. "This test exercises the Ast Capability API iterators.";
  625. return AST_TEST_NOT_RUN;
  626. case TEST_EXECUTE:
  627. break;
  628. }
  629. cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  630. if (!cap) {
  631. ast_test_status_update(test, "alloc failed\n");
  632. return AST_TEST_FAIL;
  633. }
  634. return container_test2_no_locking_helper(cap, test);
  635. }
  636. /*!
  637. * \internal
  638. */
  639. AST_TEST_DEFINE(container_test2_with_locking)
  640. {
  641. struct ast_format_cap *cap;
  642. switch (cmd) {
  643. case TEST_INIT:
  644. info->name = "container_test2_with_locking";
  645. info->category = "/main/format/";
  646. info->summary = "Test ast_format_cap iterator, with locking";
  647. info->description =
  648. "This test exercises the Ast Capability API iterators.";
  649. return AST_TEST_NOT_RUN;
  650. case TEST_EXECUTE:
  651. break;
  652. }
  653. cap = ast_format_cap_alloc(0);
  654. if (!cap) {
  655. ast_test_status_update(test, "alloc failed\n");
  656. return AST_TEST_FAIL;
  657. }
  658. return container_test2_no_locking_helper(cap, test);
  659. }
  660. static int container_test3_helper(int nolocking, struct ast_test *test)
  661. {
  662. int x;
  663. int res = AST_TEST_PASS;
  664. struct ast_format_cap *cap1;
  665. struct ast_format_cap *cap2;
  666. struct ast_format_cap *joint;
  667. for (x = 0; x < 2000; x++) {
  668. if (nolocking) {
  669. cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  670. cap2 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  671. joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  672. } else {
  673. cap1 = ast_format_cap_alloc(0);
  674. cap2 = ast_format_cap_alloc(0);
  675. joint = ast_format_cap_alloc(0);
  676. }
  677. if (!cap1 || !cap2 || !joint) {
  678. ast_test_status_update(test, "cap alloc fail\n");
  679. return AST_TEST_FAIL;
  680. }
  681. ast_format_cap_add_all(cap1);
  682. ast_format_cap_add_all_by_type(cap2, AST_FORMAT_TYPE_AUDIO);
  683. ast_format_cap_joint_copy(cap1, cap2, joint);
  684. if (!(ast_format_cap_identical(cap2, joint))) {
  685. ast_test_status_update(test, "failed identical test\n");
  686. res = AST_TEST_FAIL;
  687. cap1 = ast_format_cap_destroy(cap1);
  688. cap2 = ast_format_cap_destroy(cap2);
  689. joint = ast_format_cap_destroy(joint);
  690. break;
  691. }
  692. cap1 = ast_format_cap_destroy(cap1);
  693. cap2 = ast_format_cap_destroy(cap2);
  694. joint = ast_format_cap_destroy(joint);
  695. }
  696. return res;
  697. }
  698. /*!
  699. * \internal
  700. */
  701. AST_TEST_DEFINE(container_test3_nolock)
  702. {
  703. switch (cmd) {
  704. case TEST_INIT:
  705. info->name = "container_test3_no_locking";
  706. info->category = "/main/format/";
  707. info->summary = "Load Test ast_format_cap no locking.";
  708. info->description =
  709. "This test exercises the Ast Capability API and its iterators for the purpose "
  710. "of measuring performance.";
  711. return AST_TEST_NOT_RUN;
  712. case TEST_EXECUTE:
  713. break;
  714. }
  715. return container_test3_helper(1, test);
  716. }
  717. /*!
  718. * \internal
  719. */
  720. AST_TEST_DEFINE(container_test3_withlock)
  721. {
  722. switch (cmd) {
  723. case TEST_INIT:
  724. info->name = "container_test3_with_locking";
  725. info->category = "/main/format/";
  726. info->summary = "Load Test ast_format_cap with locking.";
  727. info->description =
  728. "This test exercises the Ast Capability API and its iterators for the purpose "
  729. "of measuring performance.";
  730. return AST_TEST_NOT_RUN;
  731. case TEST_EXECUTE:
  732. break;
  733. }
  734. return container_test3_helper(0, test);
  735. }
  736. static int unload_module(void)
  737. {
  738. AST_TEST_UNREGISTER(format_test1);
  739. AST_TEST_UNREGISTER(format_test2);
  740. AST_TEST_UNREGISTER(container_test1_nolock);
  741. AST_TEST_UNREGISTER(container_test1_withlock);
  742. AST_TEST_UNREGISTER(container_test2_no_locking);
  743. AST_TEST_UNREGISTER(container_test2_with_locking);
  744. AST_TEST_UNREGISTER(container_test3_nolock);
  745. AST_TEST_UNREGISTER(container_test3_withlock);
  746. return 0;
  747. }
  748. static int load_module(void)
  749. {
  750. AST_TEST_REGISTER(format_test1);
  751. AST_TEST_REGISTER(format_test2);
  752. AST_TEST_REGISTER(container_test1_nolock);
  753. AST_TEST_REGISTER(container_test1_withlock);
  754. AST_TEST_REGISTER(container_test2_no_locking);
  755. AST_TEST_REGISTER(container_test2_with_locking);
  756. AST_TEST_REGISTER(container_test3_nolock);
  757. AST_TEST_REGISTER(container_test3_withlock);
  758. return AST_MODULE_LOAD_SUCCESS;
  759. }
  760. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_format API Tests");