Files
freeswitch/src/mod/endpoints/mod_sofia/test/test_sofia_funcs.c
T
Dmitry Verenitsin 0b4ff451e3 [mod_sofia] Pass deflect targets with a URI scheme through unchanged (#3210)
The deflect handler prefixed `sip:` and appended the profile address to any
target not starting with `sip:`. `tel:`, `sips:` and other URIs went out as
`sip:tel:...@<ip>`. A name-addr target, as a proxied `Refer-To` or
`${sip_refer_to}` passes it, went out as `sip:<sip:...>@<ip>`, or was not sent
at all when it carried a display name, URI params or `?Replaces=`.

`sofia_deflect_build_refer_to()` now builds the `Refer-To`:
- a target that has a URI scheme or contains `<` goes out as given
- `user@host`, with or without a host port, only gains the `sip:` scheme
- a bare user or number is addressed at the profile, as before
- an empty or `NULL` target, which the `deflect` app passes when run without
  data, is rejected; the handler logs it and sends no REFER

Adds `test_sofia_deflect_build_refer_to` to `test_sofia_funcs`: rejected
targets, bare users with IPv4 and IPv6 profile addresses, `user@host` forms,
URIs with a scheme, name-addr forms, the known limitations and truncation.
2026-11-18 23:35:18 +03:00

366 lines
15 KiB
C

/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Seven Du <seven@signalwire.com>
*
*
* test_sofia_funcs.c -- tests sofia functions
*
*/
#include <switch.h>
#include <test/switch_test.h>
int protect_dest_uri(switch_caller_profile_t *cp);
switch_status_t sofia_deflect_build_refer_to(const char *target, const char *sipip, char *buf, switch_size_t buflen);
static int timeout_sec = 10;
static switch_interval_time_t delay_start_ms = 5000;
FST_CORE_EX_BEGIN("./conf", SCF_VG | SCF_USE_SQL)
FST_MODULE_BEGIN(mod_sofia, sofia)
FST_SETUP_BEGIN()
{
/* Give mod_sofia time to spinup profile threads */
if (delay_start_ms) {
switch_sleep(delay_start_ms * 1000);
delay_start_ms = 0;
}
}
FST_SETUP_END()
FST_TEARDOWN_BEGIN()
{
}
FST_TEARDOWN_END()
FST_TEST_BEGIN(test_protect_url)
{
int ret;
switch_caller_profile_t cp = { 0 };
switch_core_new_memory_pool(&cp.pool);
cp.destination_number = switch_core_strdup(cp.pool, "1234");
ret = protect_dest_uri(&cp);
fst_check(ret == 0);
fst_check_string_equals(cp.destination_number, "1234");
cp.destination_number = switch_core_strdup(cp.pool, "1234@ip");
ret = protect_dest_uri(&cp);
fst_check(ret == 0);
fst_check_string_equals(cp.destination_number, "1234@ip");
cp.destination_number = switch_core_strdup(cp.pool, "external/1234@ip");
ret = protect_dest_uri(&cp);
fst_check(ret == 0);
fst_check_string_equals(cp.destination_number, "external/1234@ip");
cp.destination_number = switch_core_strdup(cp.pool, "external/sip:1234@ip");
ret = protect_dest_uri(&cp);
fst_check(ret == 0);
fst_check_string_equals(cp.destination_number, "external/1234@ip");
cp.destination_number = switch_core_strdup(cp.pool, "external/sips:1234@ip");
ret = protect_dest_uri(&cp);
fst_check(ret == 0);
fst_check_string_equals(cp.destination_number, "external/1234@ip");
cp.destination_number = switch_core_strdup(cp.pool, "external/bryän&!杜金房@freeswitch-testing:9080");
ret = protect_dest_uri(&cp);
fst_check(ret == 1);
fst_check_string_equals(cp.destination_number, "external/bry%C3%A4n%26!%E6%9D%9C%E9%87%91%E6%88%BF@freeswitch-testing:9080");
cp.destination_number = switch_core_strdup(cp.pool, "external/" SWITCH_URL_UNSAFE "@freeswitch-testing:9080");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "before: [%s]\n", cp.destination_number);
ret = protect_dest_uri(&cp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "after: [%s]\n", cp.destination_number);
fst_check(ret == 1);
fst_check_string_equals(cp.destination_number, "external/%0D%0A%20%23%25%26%2B%3A%3B%3C%3D%3E%3F@[\\]^`{|}\"@freeswitch-testing:9080");
switch_core_destroy_memory_pool(&cp.pool);
}
FST_TEST_END()
FST_TEST_BEGIN(originate_test)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{ignore_early_media=true}sofia/internal/park@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(session);
fst_check(status == SWITCH_STATUS_SUCCESS);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(test_sofia_deflect_build_refer_to)
{
char ref_to[1024];
char short_buf[12];
size_t i;
const char *sipip = "203.0.113.5";
/* expect NULL means the target is rejected */
struct {
const char *target;
const char *expect;
} cases[] = {
{ NULL, NULL },
{ "", NULL },
/* a bare user or number is addressed at the profile */
{ "1234", switch_core_sprintf(fst_pool, "sip:1234@%s", sipip) },
{ "+15551234567", switch_core_sprintf(fst_pool, "sip:+15551234567@%s", sipip) },
/* user@host only gains the scheme, including a host port or an IPv6 literal */
{ "bob@example.com", "sip:bob@example.com" },
{ "bob@198.51.100.7:2222", "sip:bob@198.51.100.7:2222" },
{ "bob@[2001:db8::7]", "sip:bob@[2001:db8::7]" },
/* a URI with a scheme goes out as given */
{ "sip:bob@example.com", "sip:bob@example.com" },
{ "sips:bob@example.com", "sips:bob@example.com" },
{ "SIPS:bob@example.com", "SIPS:bob@example.com" },
{ "sip:bob@198.51.100.7:2222", "sip:bob@198.51.100.7:2222" },
{ "tel:+15551234567", "tel:+15551234567" },
{ "urn:service:sos", "urn:service:sos" },
/* a name-addr goes out as given */
{ "<sip:bob@example.com>", "<sip:bob@example.com>" },
{ "<sip:bob@example.com;user=phone>", "<sip:bob@example.com;user=phone>" },
{ "\"Bob\" <sip:bob@example.com>", "\"Bob\" <sip:bob@example.com>" },
{ "<sip:bob@example.com?Replaces=xfer-42%3Bto-tag%3Dt1%3Bfrom-tag%3Df1>", "<sip:bob@example.com?Replaces=xfer-42%3Bto-tag%3Dt1%3Bfrom-tag%3Df1>" },
{ "<sip:bob@example.com>;x-leg=b", "<sip:bob@example.com>;x-leg=b" },
/* Known limitations: a host:port without a user, or a user:password, reads as a scheme or is
* addressed at the profile, and none of these is a valid Refer-To */
{ "pbx.example.com:5060", "pbx.example.com:5060" },
{ "198.51.100.7:5060", switch_core_sprintf(fst_pool, "sip:198.51.100.7:5060@%s", sipip) },
{ "bob:secret@example.com", "bob:secret@example.com" },
};
for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
switch_status_t status;
ref_to[0] = '\0';
status = sofia_deflect_build_refer_to(cases[i].target, sipip, ref_to, sizeof(ref_to));
if (!cases[i].expect) {
fst_xcheck(status == SWITCH_STATUS_FALSE, "an empty deflect target must be rejected");
continue;
}
fst_xcheck(status == SWITCH_STATUS_SUCCESS, cases[i].target);
fst_check_string_equals(ref_to, cases[i].expect);
}
/* An IPv6 profile address is bracketed */
sofia_deflect_build_refer_to("1234", "2001:db8:cafe::5", ref_to, sizeof(ref_to));
fst_check_string_equals(ref_to, "sip:1234@[2001:db8:cafe::5]");
/* The result is truncated to buflen on every branch */
sofia_deflect_build_refer_to("1234567", sipip, short_buf, sizeof(short_buf));
fst_check_string_equals(short_buf, "sip:1234567");
sofia_deflect_build_refer_to("bob@example.com", sipip, short_buf, sizeof(short_buf));
fst_check_string_equals(short_buf, "sip:bob@exa");
sofia_deflect_build_refer_to("sip:bob@example.com", sipip, short_buf, sizeof(short_buf));
fst_check_string_equals(short_buf, "sip:bob@exa");
}
FST_TEST_END()
#if HAVE_STIRSHAKEN
FST_TEST_BEGIN(sofia_verify_identity_test_no_identity)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{ignore_early_media=true}sofia/internal/verifyidentity@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status != SWITCH_STATUS_SUCCESS);
fst_check(cause == SWITCH_CAUSE_NO_IDENTITY);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_verify_identity_test_bad_identity)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{ignore_early_media=true,sip_h_identity=foo;info=bar}sofia/internal/verifyidentity@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status != SWITCH_STATUS_SUCCESS);
fst_check(cause == SWITCH_CAUSE_INVALID_IDENTITY);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_verify_identity_test_valid_identity_no_cert_available)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231234,ignore_early_media=true,sip_h_identity=eyJhbGciOiJFUzI1NiIsInBwdCI6InNoYWtlbiIsInR5cCI6InBhc3Nwb3J0IiwieDV1IjoiaHR0cDovLzEyNy4wLjAuMS80MDQucGVtIn0.eyJhdHRlc3QiOiJBIiwiZGVzdCI6eyJ0biI6WyIxNTU1MzIxNDMyMSJdfSwiaWF0IjoxNjE4Mjc5OTYzLCJvcmlnIjp7InRuIjoiMTU1NTEyMzEyMzQifSwib3JpZ2lkIjoiMTMxMzEzMTMifQ.Cm34sISkFWYB6ohtjjJEO71Hyz4TQ5qrTDyYmCXBj-ni5Fe7IbNjmMyvY_lD_Go0u2csWQNe8n03fHSO7Z7nNw;info=<http://127.0.0.1/404.pem>;alg=ES256;ppt=shaken}sofia/internal/+15553214321@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status != SWITCH_STATUS_SUCCESS);
fst_check(cause == SWITCH_CAUSE_INVALID_IDENTITY);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_auth_identity_test_attest_a)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231234,ignore_early_media=true,sip_stir_shaken_attest=A}sofia/internal/+15553214322@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status == SWITCH_STATUS_SUCCESS);
fst_check(session);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_auth_identity_test_attest_b)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231234,ignore_early_media=true,sip_stir_shaken_attest=B}sofia/internal/+15553214322@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status == SWITCH_STATUS_SUCCESS);
fst_check(session);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_auth_identity_test_attest_c)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231234,ignore_early_media=true,sip_stir_shaken_attest=C}sofia/internal/+15553214322@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status == SWITCH_STATUS_SUCCESS);
fst_check(session);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_verify_identity_test_verified_attest_a_expired)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231234,ignore_early_media=true,sip_h_identity=eyJhbGciOiJFUzI1NiIsInBwdCI6InNoYWtlbiIsInR5cCI6InBhc3Nwb3J0IiwieDV1IjoiaHR0cDovLzEyNy4wLjAuMTo4MDgwL2NlcnQucGVtIn0.eyJhdHRlc3QiOiJBIiwiZGVzdCI6eyJ0biI6WyIxNTU1MzIxNDMyMiJdfSwiaWF0IjoxNjE4MzczMTc0LCJvcmlnIjp7InRuIjoiMTU1NTEyMzEyMzQifSwib3JpZ2lkIjoiMzliZDYzZDQtOTE1Mi00MzU0LWFkNjctNjg5NjQ2NmI4ZDI3In0.mUaikwHSOb8RVPwwMZTsqBe57MZY29CgbIqmiiEmyq9DzKZO-y4qShiIVT3serg-xHgC9SCMjUOBWaDfeXnEvA;info=<http://127.0.0.1:8080/cert.pem>;alg=ES256;ppt=shaken}sofia/internal/+15553214322@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status != SWITCH_STATUS_SUCCESS);
fst_check(cause == SWITCH_CAUSE_CALL_REJECTED);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(1 * 1000 * 1000);
}
}
FST_TEST_END()
FST_TEST_BEGIN(sofia_auth_identity_test_attest_a_date)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
const char *local_ip_v4 = switch_core_get_variable("local_ip_v4");
status = switch_ivr_originate(NULL, &session, &cause, switch_core_sprintf(fst_pool, "{origination_caller_id_number=+15551231235,ignore_early_media=true,sip_stir_shaken_attest=A}sofia/internal/+15553214323@%s:53060", local_ip_v4), timeout_sec, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
fst_check(status == SWITCH_STATUS_SUCCESS);
fst_check(session);
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_sleep(10 * 1000 * 1000);
}
}
FST_TEST_END()
#endif
FST_MODULE_END()
FST_CORE_END()
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/