]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
test_res_pjsip_session_caps: Create unit test
authorGeorge Joseph <gjoseph@digium.com>
Wed, 25 Mar 2020 17:51:32 +0000 (11:51 -0600)
committerGeorge Joseph <gjoseph@digium.com>
Mon, 6 Apr 2020 13:02:53 +0000 (08:02 -0500)
This unit test runs through combinations of...
* Local codecs
* Remote Codecs
* Codec Preference
* Incoming/Outgoing

A few new APIs were created to make it easier to test
the functionality but didn't result in any actual
functional change.

ASTERISK_28777

Change-Id: Ic8957c43e7ceeab0e9272af60ea53f056164f164

include/asterisk/res_pjsip.h
res/res_pjsip.c
res/res_pjsip/pjsip_configuration.c
res/res_pjsip_session.exports.in
tests/test_res_pjsip_session_caps.c [new file with mode: 0644]

index c1d5490f0b8db4212d443095e15a2161cf9459f7..fd80581003f0ae9d719ee5683ce0e7807fe72de1 100644 (file)
@@ -3254,6 +3254,19 @@ int ast_sip_str_to_dtmf(const char *dtmf_mode);
  */
 const char *ast_sip_call_codec_pref_to_str(struct ast_flags pref);
 
+/*!
+ * \brief Convert a call codec preference string to preference flags
+ * \since 18.0.0
+ *
+ * \param pref A pointer to an ast_flags structure to receive the preference flags
+ * \param pref_str The call codec preference setting string
+ * \param is_outgoing Is for outgoing calls?
+ *
+ * \retval 0 The string was parsed successfully
+ * \retval -1 The string option was invalid
+ */
+int ast_sip_call_codec_str_to_pref(struct ast_flags *pref, const char *pref_str, int is_outgoing);
+
 /*!
  * \brief Transport shutdown monitor callback.
  * \since 13.18.0
index 12e41cc868530710ad71211568b5112794dc2b04..1db5e7b812fb79b1cd2c4dfc4b870e0b0ee0dc6e 100644 (file)
@@ -5117,6 +5117,29 @@ const char *ast_sip_call_codec_pref_to_str(struct ast_flags pref)
        return value;
 }
 
+int ast_sip_call_codec_str_to_pref(struct ast_flags *pref, const char *pref_str, int is_outgoing)
+{
+       pref->flags = 0;
+
+       if (strcmp(pref_str, "local") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_ALL);
+       } else if (is_outgoing && strcmp(pref_str, "local_merge") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_ALL);
+       } else if (strcmp(pref_str, "local_first") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_FIRST);
+       } else if (strcmp(pref_str, "remote") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_ALL);
+       } else if (is_outgoing && strcmp(pref_str, "remote_merge") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_ALL);
+       } else if (strcmp(pref_str, "remote_first") == 0) {
+               ast_set_flag(pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_FIRST);
+       } else {
+               return -1;
+       }
+
+       return 0;
+}
+
 /*!
  * \brief Set name and number information on an identity header.
  *
index 5a4842b159e5c0c41fe3f0dcaedf4f38e5240752..f95ee9e2409eff6bf9f2fc5dd4d0685fe922903d 100644 (file)
@@ -1128,19 +1128,8 @@ static int call_offer_pref_handler(const struct aco_option *opt,
        struct ast_flags pref = { 0, };
        int outgoing = strcmp(var->name, "outgoing_call_offer_pref") == 0;
 
-       if (strcmp(var->value, "local") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_ALL);
-       } else if (outgoing && strcmp(var->value, "local_merge") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_ALL);
-       } else if (strcmp(var->value, "local_first") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_FIRST);
-       } else if (strcmp(var->value, "remote") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_ALL);
-       } else if (outgoing && strcmp(var->value, "remote_merge") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_ALL);
-       } else if (strcmp(var->value, "remote_first") == 0) {
-               ast_set_flag(&pref, AST_SIP_CALL_CODEC_PREF_REMOTE | AST_SIP_CALL_CODEC_PREF_UNION | AST_SIP_CALL_CODEC_PREF_FIRST);
-       } else {
+       int res = ast_sip_call_codec_str_to_pref(&pref, var->value, outgoing);
+       if (res != 0) {
                return -1;
        }
 
index d65b247c7a1248a5757c518d3d23fc70993c0f64..65c35addc83cb038687e84b76807b981d16de043 100644 (file)
@@ -4,6 +4,7 @@
                LINKER_SYMBOL_PREFIX__ast_sip_session_*;
                LINKER_SYMBOL_PREFIXast_sip_dialog_get_session;
                LINKER_SYMBOL_PREFIXast_sip_channel_pvt_alloc;
+               LINKER_SYMBOL_PREFIXast_sip_create_joint_call_cap;
        local:
                *;
 };
diff --git a/tests/test_res_pjsip_session_caps.c b/tests/test_res_pjsip_session_caps.c
new file mode 100644 (file)
index 0000000..54438f5
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2020, Sangoma Technologies Corporation
+ *
+ * George Joseph <gjoseph@sangoma.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief res_pjsip_session format caps tests
+ *
+ * \author George Joseph <gjoseph@sangoma.com>
+ *
+ */
+
+/*** MODULEINFO
+       <depend>TEST_FRAMEWORK</depend>
+       <depend>pjproject</depend>
+       <depend>res_pjsip</depend>
+       <depend>res_pjsip_session</depend>
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/res_pjsip.h"
+#include "asterisk/utils.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cap.h"
+#include "asterisk/res_pjsip_session.h"
+#include "asterisk/res_pjsip_session_caps.h"
+
+static enum ast_test_result_state test_create_joint(struct ast_test *test, const char *local_string,
+       const char *remote_string, const char *pref_string, int is_outgoing, const char *expected_string,
+       enum ast_test_result_state expected_result)
+{
+       RAII_VAR(struct ast_format_cap *, local, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
+       RAII_VAR(struct ast_format_cap *, remote, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
+       RAII_VAR(struct ast_format_cap *, joint, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
+       struct ast_str *joint_str = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+       const char *joint_string;
+       char *stripped_joint;
+       struct ast_flags codec_prefs;
+       int rc;
+       int i;
+
+       ast_test_status_update(test, "Testing local: (%s), remote: (%s), pref: (%-12s), outgoing: (%s), expected: (%s) expected result: (%s)\n",
+               local_string, remote_string, pref_string, is_outgoing ? "yes" : "no ", expected_string,
+               expected_result == AST_TEST_PASS ? "PASS" : "FAIL");
+
+       ast_test_validate(test, local != NULL && remote != NULL && joint != NULL);
+
+       rc = ast_format_cap_update_by_allow_disallow(local, local_string, 1);
+       if (rc != 0) {
+               ast_test_status_update(test, "    %sxpected Failure: Coulldn't parse local codecs (%s)\n",
+                       expected_result == AST_TEST_FAIL ? "E" : "Une", local_string);
+               return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
+       }
+       rc = ast_format_cap_update_by_allow_disallow(remote, remote_string, 1);
+       if (rc != 0) {
+               ast_test_status_update(test, "    %sxpected Failure: Coulldn't parse remote codecs (%s)\n",
+                       expected_result == AST_TEST_FAIL ? "E" : "Une", remote_string);
+               return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
+       }
+
+       rc = ast_sip_call_codec_str_to_pref(&codec_prefs, pref_string, is_outgoing);
+       if (rc != 0) {
+               ast_test_status_update(test, "    %sxpected Failure: Invalid preference string incoming/outgoing combination.\n",
+                       expected_result == AST_TEST_FAIL ? "E" : "Une");
+               return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
+       }
+
+       joint = ast_sip_create_joint_call_cap(remote, local, AST_MEDIA_TYPE_AUDIO, codec_prefs);
+       if (joint == NULL) {
+               ast_test_status_update(test, "    %sxpected Failure: No joint caps.\n",
+                       expected_result == AST_TEST_FAIL ? "E" : "Une");
+               return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
+       }
+
+       joint_string = ast_format_cap_get_names(joint, &joint_str);
+       stripped_joint = ast_str_truncate(joint_str, ast_str_strlen(joint_str) - 1) + 1;
+       for(i = 0; i <= strlen(stripped_joint); i++) {
+               if(stripped_joint[i] == '|') {
+                       stripped_joint[i] = ',';
+               }
+       }
+
+       if (!joint_string || strcmp(stripped_joint, expected_string) != 0) {
+               ast_test_status_update(test, "    %sxpected Failure: Expected: (%s) Actual: (%s)\n",
+                       expected_result == AST_TEST_FAIL ? "E" : "Une", expected_string, stripped_joint);
+               return expected_result == AST_TEST_FAIL ? AST_TEST_PASS : AST_TEST_FAIL;
+       }
+
+       return AST_TEST_PASS;
+}
+
+#define RUN_CREATE_JOINT(local, remote, pref, outgoing, expected, result) \
+do { \
+       if (test_create_joint(test, local, remote, pref, outgoing, expected, result) != AST_TEST_PASS) { \
+               rc += 1; \
+       } \
+} while (0)
+
+AST_TEST_DEFINE(low_level)
+{
+       int rc = 0;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = __func__;
+               info->category = "/res/res_pjsip_session/caps/";
+               info->summary = "Test res_pjsip_session_caps";
+               info->description = "Test res_pjsip_session_caps";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       /* Incoming */
+
+       ast_test_status_update(test, "Testing incoming expected pass\n");
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "local",                0,      "alaw,g722",    AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "local_first",  0,      "alaw",                 AST_TEST_PASS);
+       RUN_CREATE_JOINT("slin",                        "all",                          "local",                0,      "slin",                 AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "remote",               0,      "g722,alaw",    AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "remote_first", 0,      "g722",                 AST_TEST_PASS);
+       RUN_CREATE_JOINT("all",                         "slin",                         "remote_first", 0,      "slin",                 AST_TEST_PASS);
+
+       ast_test_status_update(test, "Testing incoming expected fail\n");
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g729",                         "local",                0,      "",                             AST_TEST_FAIL);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "local_merge",  0,      "",                             AST_TEST_FAIL);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,alaw,g729",       "remote_merge", 0,      "",                             AST_TEST_FAIL);
+
+       ast_test_status_update(test, "Testing outgoing expected pass\n");
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "local",                1,      "alaw,g722",                    AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "local_first",  1,      "alaw",                                 AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "local_merge",  1,      "ulaw,alaw,g722,g729",  AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "remote",               1,      "g722,alaw",                    AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "remote_first", 1,      "g722",                                 AST_TEST_PASS);
+       RUN_CREATE_JOINT("ulaw,alaw,g722",      "g722,g729,alaw",       "remote_merge", 1,      "g722,g729,alaw,ulaw",  AST_TEST_PASS);
+       RUN_CREATE_JOINT("!all",                        "g722,g729,alaw",       "remote_merge", 1,      "g722,g729,alaw",               AST_TEST_PASS);
+
+       return rc >= 1 ? AST_TEST_FAIL : AST_TEST_PASS;
+}
+
+static int load_module(void)
+{
+       AST_TEST_REGISTER(low_level);
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+       AST_TEST_UNREGISTER(low_level);
+       return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "res_pjsip_session caps test module",
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .requires = "res_pjsip_session",
+);