def test_stateless_proxy_forward_to_ua(): def check_rroute_fun(uri): return uri == Uri(this_proxy_uri) bob_uri = 'sip:[email protected]' this_proxy_uri = 'sip:this.proxy.org' msg = 'INVITE ' + bob_uri + ' SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nRoute: <' + this_proxy_uri + '>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' proxy_opts = ProxyOptionsDetails(check_rroute_function=check_rroute_fun) options = ValidationOptions(proxy=proxy_opts) sip_msg = process_route_info(raw_message(msg), options) target = Uri(bob_uri) sip_msg, opts = CommonProxy.forward_request(target, sip_msg, proxy_opts) assert target == opts.nexthop assert isinstance(sip_msg.find(ROUTE_HEADER), NotFound)
def test_process_route_info_loose_routing(): def check_rroute_fun(uri): return Uri(this_proxy_uri) == uri bob_uri = 'sip:[email protected]' this_proxy_uri = 'sip:this.proxy.org' next_proxy_uri = 'sip:next.proxy.org' msg = 'INVITE ' + bob_uri + ' SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nRoute: <' + this_proxy_uri + '>' + \ '\r\nRoute: <' + next_proxy_uri + '>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' options = ValidationOptions(proxy=ProxyOptionsDetails(check_rroute_function=check_rroute_fun)) sip_msg = process_route_info(raw_message(msg), options) sip_msg_rebuilt = rebuild_sip_msg(sip_msg) assert sip_msg_rebuilt.ruri == Uri(bob_uri) expected_route_hdr = RouteHeader(next_proxy_uri) assert expected_route_hdr == sip_msg_rebuilt.get(ROUTE_HEADER)
def test_forward_request_record_route_append(): bob_uri = 'sip:[email protected]' another_proxy_uri = 'sip:another.proxy.org;lr' this_proxy_uri = Uri('sip:this.proxy.org') msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nRecord-Route: <' + another_proxy_uri + '>' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' proxy_opts = ProxyOptionsDetails(record_route_uri=this_proxy_uri, check_rroute_function=lambda x: this_proxy_uri == x) sip_msg, opts = forward_request(bob_uri, raw_message(msg), proxy_opts) sip_msg = rebuild_sip_msg(sip_msg) record_route_hdr = sip_msg.get(RECORD_ROUTE_HEADER) first_rr_uri = record_route_hdr.first.uri last_rr_uri = record_route_hdr.last.uri this_proxy_uri.set_param('lr', True) assert first_rr_uri == this_proxy_uri assert last_rr_uri == Uri(another_proxy_uri)
def test_forward_request_add_maxforwars(): bob_uri = 'sip:[email protected]' msg = 'INVITE ' + bob_uri + ' SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' sip_msg, options = forward_request(bob_uri, raw_message(msg), ProxyOptionsDetails()) sip_msg = rebuild_sip_msg(sip_msg) assert sip_msg.get(MAXFORWARDS_HEADER).maxforwards == 70
def test_proxy_params_check_no_validate(): msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' this_proxy_uri = Uri('sip:this.proxy.org') options = ValidationOptions(proxy=ProxyOptionsDetails(record_route_uri=this_proxy_uri, no_validate=True)) # No exceptions raised: process_route_info(raw_message(msg), options)
def test_request_validation_proxy_require_all_supported(): msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nProxy-Require: gin' + \ '\r\n\r\n' supported = OptTagListHeader.from_list([OptionTag('gin')]) options = ValidationOptions(proxy=ProxyOptionsDetails(supported=supported)) # no errors while validating: response = CommonProxy.validate_request(raw_message(msg), options) assert isinstance(response, SipMessage)
def test_forward_request_no_rr_sips_to_sips(): bob_uri = 'sips:[email protected]' msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' raw_msg = raw_message(msg) raw_msg.source = tls_source sip_msg, opts = forward_request(bob_uri, raw_msg, ProxyOptionsDetails()) sip_msg = rebuild_sip_msg(sip_msg) assert isinstance(sip_msg.find(RECORD_ROUTE_HEADER), NotFound)
def test_forward_request_invalid_maxforwars(): bob_uri = 'sip:[email protected]' msg = 'INVITE ' + bob_uri + ' SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: x' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' sip_msg = SipMessage.parse(raw_message(msg), []) with pytest.raises(SipHeaderError) as exc: CommonProxy.forward_request(Uri(bob_uri), sip_msg, ProxyOptionsDetails()) assert 'max-forwards' in str(exc.value).lower()
def test_process_route_info_no_routes_strict_route(): bob_uri = 'sip:[email protected]' msg = 'INVITE ' + bob_uri + ' SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' options = ValidationOptions(proxy=ProxyOptionsDetails(check_rroute_function=lambda x: True)) sip_msg = process_route_info(raw_message(msg), options) sip_msg = rebuild_sip_msg(sip_msg) assert sip_msg.ruri == Uri(bob_uri) assert isinstance(sip_msg.find(ROUTE_HEADER), NotFound)
def test_forward_request_to_loose_router(): bob_uri = 'sip:[email protected]' loose_route_uri = 'sip:stict.proxy.org;lr' msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nRoute: <' + loose_route_uri + '>' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' sip_msg, opts = forward_request(bob_uri, raw_message(msg), ProxyOptionsDetails()) expected_nexthop = Uri(loose_route_uri) assert opts.nexthop == expected_nexthop sip_msg = rebuild_sip_msg(sip_msg) assert sip_msg.ruri == Uri(bob_uri)
def test_forward_request_no_rr_sip_to_sips(): bob_uri = 'sips:[email protected]' another_proxy_uri = 'sip:another.proxy.org;lr' msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nRecord-Route: <' + another_proxy_uri + '>' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' raw_msg = raw_message(msg) raw_msg.source = udp_source with pytest.raises(CommonProxyError) as exc: forward_request(bob_uri, raw_msg, ProxyOptionsDetails()) assert 'sips transform' in str(exc.value).lower()
def test_request_validation_proxy_require_no_required(): msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 70' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nProxy-Require: gin' + \ '\r\n\r\n' unsupported = OptTagListHeader.from_list([OptionTag('gin')]) supported = OptTagListHeader.from_list([OptionTag('100rel'), OptionTag('timers')]) options = ValidationOptions(proxy=ProxyOptionsDetails(supported=supported)) response = CommonProxy.validate_request(raw_message(msg), options) assert response.status == 420 assert response.reason.lower() == 'bad extension' resp_raw_message = raw_message(response.serialize()) sip_response = SipMessage.parse(resp_raw_message, [UNSUPPORTED_HEADER]) assert sip_response.get(UNSUPPORTED_HEADER) == unsupported
def test_forward_request_record_route_append_sips(): # If the Request-URI contains a SIPS URI, or the topmost Route header field value (after the post processing of # bullet 6) contains a SIPS URI, the URI placed into the Record-Route header field MUST be a SIPS URI. bob_uri = 'sips:[email protected]' another_proxy_uri = 'sip:another.proxy.org;lr' msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nRecord-Route: <' + another_proxy_uri + '>' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' this_proxy_uri = Uri('sips:this.proxy.org') proxy_opts = ProxyOptionsDetails(record_route_uri=this_proxy_uri, check_rroute_function=lambda x: x == this_proxy_uri) forward_request(bob_uri, raw_message(msg), proxy_opts)
def test_request_validation_maxforwards_is_zero_options_reply(): msg = 'OPTIONS sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nMax-Forwards: 0' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 OPTIONS' + \ '\r\n\r\n' allow_methods = AllowHeader(MethodSet([INVITE, ACK, OPTIONS, CANCEL, BYE])) supported_list = OptTagListHeader.from_list([OptionTag('100rel'), OptionTag('timers')]) options = ValidationOptions(validate=ValidationOptionsDetails(reply_on_options=True), proxy=ProxyOptionsDetails(supported=supported_list, allow=allow_methods)) response = CommonProxy.validate_request(raw_message(msg), options) assert response.status == 200 assert response.reason == 'OK' resp_raw_message = raw_message(response.serialize()) sip_response = SipMessage.parse(resp_raw_message, [ALLOW_HEADER, SUPPORTED_HEADER]) assert allow_methods == sip_response.get(ALLOW_HEADER) assert sip_response.get(SUPPORTED_HEADER) == supported_list
def test_forward_request_no_rr_sips_to_sip(): # If the Request-URI contains a SIPS URI, or the topmost Route header field value (after the post processing of # bullet 6) contains a SIPS URI, the URI placed into the Record-Route header field MUST be a SIPS URI. bob_uri = 'sip:[email protected]' another_proxy_uri = 'sip:another.proxy.org;lr' msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nVia: SIP/2.0/UDP bigbox3.site3.atlanta.com' + \ '\r\nTo: Bob <sip:[email protected]>' + \ '\r\nFrom: Alice <sip:[email protected]>;tag=1928301774' + \ '\r\nCall-ID: [email protected]' + \ '\r\nCSeq: 314159 INVITE' + \ '\r\nContact: <sip:[email protected]>' + \ '\r\nContent-Type: application/sdp' + \ '\r\nRecord-Route: <' + another_proxy_uri + '>' + \ '\r\nContent-Length: 4' + \ '\r\n\r\nTest' raw_msg = raw_message(msg) raw_msg.source = tls_source with pytest.raises(CommonProxyError) as exc: forward_request(bob_uri, raw_msg, ProxyOptionsDetails()) assert 'sip transform' in str(exc.value).lower()