test_res_pjsip_session_caps.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2020, Sangoma Technologies Corporation
  5. *
  6. * George Joseph <gjoseph@sangoma.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 res_pjsip_session format caps tests
  21. *
  22. * \author George Joseph <gjoseph@sangoma.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <depend>pjproject</depend>
  28. <depend>res_pjsip</depend>
  29. <depend>res_pjsip_session</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/res_pjsip.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/format.h"
  38. #include "asterisk/format_cap.h"
  39. #include "asterisk/res_pjsip_session.h"
  40. #include "asterisk/res_pjsip_session_caps.h"
  41. static enum ast_test_result_state test_create_joint(struct ast_test *test, const char *local_string,
  42. const char *remote_string, const char *pref_string, int is_outgoing, const char *expected_string,
  43. enum ast_test_result_state expected_result)
  44. {
  45. RAII_VAR(struct ast_format_cap *, local, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  46. RAII_VAR(struct ast_format_cap *, remote, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  47. RAII_VAR(struct ast_format_cap *, joint, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
  48. struct ast_str *joint_str = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  49. const char *joint_string;
  50. char *stripped_joint;
  51. struct ast_flags codec_prefs;
  52. int rc;
  53. int i;
  54. ast_test_status_update(test, "Testing local: (%s), remote: (%s), pref: (%-12s), outgoing: (%s), expected: (%s) expected result: (%s)\n",
  55. local_string, remote_string, pref_string, is_outgoing ? "yes" : "no ", expected_string,
  56. expected_result == AST_TEST_PASS ? "PASS" : "FAIL");
  57. ast_test_validate(test, local != NULL && remote != NULL && joint != NULL);
  58. rc = ast_format_cap_update_by_allow_disallow(local, local_string, 1);
  59. if (rc != 0) {
  60. ast_test_status_update(test, " %sxpected Failure: Coulldn't parse local codecs (%s)\n",
  61. expected_result == AST_TEST_FAIL ? "E" : "Une", local_string);
  62. return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
  63. }
  64. rc = ast_format_cap_update_by_allow_disallow(remote, remote_string, 1);
  65. if (rc != 0) {
  66. ast_test_status_update(test, " %sxpected Failure: Coulldn't parse remote codecs (%s)\n",
  67. expected_result == AST_TEST_FAIL ? "E" : "Une", remote_string);
  68. return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
  69. }
  70. rc = ast_sip_call_codec_str_to_pref(&codec_prefs, pref_string, is_outgoing);
  71. if (rc != 0) {
  72. ast_test_status_update(test, " %sxpected Failure: Invalid preference string incoming/outgoing combination.\n",
  73. expected_result == AST_TEST_FAIL ? "E" : "Une");
  74. return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
  75. }
  76. joint = ast_sip_create_joint_call_cap(remote, local, AST_MEDIA_TYPE_AUDIO, codec_prefs);
  77. if (joint == NULL) {
  78. ast_test_status_update(test, " %sxpected Failure: No joint caps.\n",
  79. expected_result == AST_TEST_FAIL ? "E" : "Une");
  80. return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
  81. }
  82. joint_string = ast_format_cap_get_names(joint, &joint_str);
  83. stripped_joint = ast_str_truncate(joint_str, ast_str_strlen(joint_str) - 1) + 1;
  84. for(i = 0; i <= strlen(stripped_joint); i++) {
  85. if(stripped_joint[i] == '|') {
  86. stripped_joint[i] = ',';
  87. }
  88. }
  89. if (!joint_string || strcmp(stripped_joint, expected_string) != 0) {
  90. ast_test_status_update(test, " %sxpected Failure: Expected: (%s) Actual: (%s)\n",
  91. expected_result == AST_TEST_FAIL ? "E" : "Une", expected_string, stripped_joint);
  92. return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
  93. }
  94. return AST_TEST_PASS;
  95. }
  96. #define RUN_CREATE_JOINT(local, remote, pref, outgoing, expected, result) \
  97. do { \
  98. if (test_create_joint(test, local, remote, pref, outgoing, expected, result) != AST_TEST_PASS) { \
  99. rc += 1; \
  100. } \
  101. } while (0)
  102. AST_TEST_DEFINE(low_level)
  103. {
  104. int rc = 0;
  105. switch (cmd) {
  106. case TEST_INIT:
  107. info->name = __func__;
  108. info->category = "/res/res_pjsip_session/caps/";
  109. info->summary = "Test res_pjsip_session_caps";
  110. info->description = "Test res_pjsip_session_caps";
  111. return AST_TEST_NOT_RUN;
  112. case TEST_EXECUTE:
  113. break;
  114. }
  115. /* Incoming */
  116. ast_test_status_update(test, "Testing incoming expected pass\n");
  117. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "local", 0, "alaw,g722", AST_TEST_PASS);
  118. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "local_first", 0, "alaw", AST_TEST_PASS);
  119. RUN_CREATE_JOINT("slin", "all", "local", 0, "slin", AST_TEST_PASS);
  120. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "remote", 0, "g722,alaw", AST_TEST_PASS);
  121. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "remote_first", 0, "g722", AST_TEST_PASS);
  122. RUN_CREATE_JOINT("all", "slin", "remote_first", 0, "slin", AST_TEST_PASS);
  123. ast_test_status_update(test, "Testing incoming expected fail\n");
  124. RUN_CREATE_JOINT("ulaw,alaw,g722", "g729", "local", 0, "", AST_TEST_FAIL);
  125. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "local_merge", 0, "", AST_TEST_FAIL);
  126. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,alaw,g729", "remote_merge", 0, "", AST_TEST_FAIL);
  127. ast_test_status_update(test, "Testing outgoing expected pass\n");
  128. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "local", 1, "alaw,g722", AST_TEST_PASS);
  129. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "local_first", 1, "alaw", AST_TEST_PASS);
  130. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "local_merge", 1, "ulaw,alaw,g722", AST_TEST_PASS);
  131. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "remote", 1, "g722,alaw", AST_TEST_PASS);
  132. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "remote_first", 1, "g722", AST_TEST_PASS);
  133. RUN_CREATE_JOINT("ulaw,alaw,g722", "g722,g729,alaw", "remote_merge", 1, "g722,alaw,ulaw", AST_TEST_PASS);
  134. RUN_CREATE_JOINT("!all", "g722,g729,alaw", "remote_merge", 1, "nothing", AST_TEST_PASS);
  135. return rc >= 1 ? AST_TEST_FAIL : AST_TEST_PASS;
  136. }
  137. static int load_module(void)
  138. {
  139. AST_TEST_REGISTER(low_level);
  140. return AST_MODULE_LOAD_SUCCESS;
  141. }
  142. static int unload_module(void)
  143. {
  144. AST_TEST_UNREGISTER(low_level);
  145. return 0;
  146. }
  147. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "res_pjsip_session caps test module",
  148. .support_level = AST_MODULE_SUPPORT_CORE,
  149. .load = load_module,
  150. .unload = unload_module,
  151. .requires = "res_pjsip_session",
  152. );