def test_make_sip(): expected_uri2 = Uri(b'sip:[email protected]:5061') uri2 = Uri.make(scheme=b'sip', user=b'Alice', host=b'atlanta.com', port=b'5061') assert expected_uri2 == uri2
def test_make_sips_unicode(): expected_uri = Uri(u'sips:[email protected]:8083') uri = Uri.make(scheme=u'sips', user=u'Alice', host=u'atlanta.com', port=8083) assert expected_uri == uri
def test_make_sip_unicode(): expected_uri2 = Uri(u'sip:[email protected]:5061') uri2 = Uri.make(scheme=u'sip', user=u'Alice', host=u'atlanta.com', port=u'5061') assert expected_uri2 == uri2
def test_make_sips(): expected_uri = Uri(b'sips:[email protected]:8083') uri = Uri.make(scheme=b'sips', user=b'Alice', host=b'atlanta.com', port=8083) assert expected_uri == uri
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_compare_ne(): assert Uri(b'SIP:[email protected];Transport=udp') != Uri( b'sip:[email protected];Transport=UDP') assert Uri(b'sip:[email protected]') != Uri(b'sip:[email protected]:5060') assert Uri(b'sip:[email protected]') != Uri( b'sip:[email protected];transport=udp') assert Uri(b'sip:[email protected]') != Uri( b'sip:[email protected]:6000;transport=tcp') assert Uri(b'sip:[email protected]') != Uri( b'sip:[email protected]?Subject=next%20meeting')
def test_nameaddr_parse(na_str, host, user, display_name, rest): parsed_na = NameAddress(na_str) host = PARSER.validate_host(host) uri = Uri() uri.parser_impl = PARSER uri.user = user uri.host = host assert parsed_na.display_name == display_name assert parsed_na.uri == uri assert parsed_na.rest == rest
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_parse_tag(display_name, tag, fromto): uri_str = 'sip:[email protected]' uri = Uri(uri_str) fromto_hdr = FromToHeader(fromto) assert fromto_hdr.display_name == display_name assert fromto_hdr.uri == uri assert fromto_hdr.tag == tag
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_parse_param(): uri_str = 'sip:[email protected]' uri = Uri(uri_str) fromto_hdr = FromToHeader('<sip:[email protected]>;Some=y') hp = HParams() hp.set_raw('Some', 'y') assert fromto_hdr.params == hp assert fromto_hdr.uri == uri
def test_three_params(): uri = Uri( b'sip:[email protected];param1=value1;param2=value2;param3=value3') assert { 'param1': 'value1', 'param2': 'value2', 'param3': 'value3' } == uri.params
def __init__(self, contact_string=None): self.display_name = None self.uri = Uri() self.uri.parser_impl = SIPUriParserUnicode() self.params = HParams() self.rest = None if contact_string is not None: self.display_name, self.uri, self.params, self.rest = self.parse_hdr( contact_string)
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_set_ruri(): 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' sip_msg, options = forward_request(bob_uri, raw_message(msg), ProxyOptionsDetails()) expected_nexthop = Uri(bob_uri) assert options.nexthop == expected_nexthop sip_msg = rebuild_sip_msg(sip_msg) assert sip_msg.get(MAXFORWARDS_HEADER).maxforwards == 69 assert sip_msg.ruri == Uri(bob_uri)
def ruri_from_raw(raw_message): """Returns None if raw message has type response and Uri object if raw message has type request. Args: raw_message (Message) Returns: :obj:Uri or None """ if raw_message.type == TYPE_RESPONSE: return None elif raw_message.type == TYPE_REQUEST: return Uri(raw_message.ruri)
def parse(string): try: display_name, rest = NameAddress.parse_display_name(string) if isinstance(display_name, list) and len(display_name) == 1: display_name = display_name[0] address_match = NameAddress.BRACKETS_ADDRESS_RX.match(rest) if address_match: address, rest = address_match.group(1), address_match.group(2) uri = Uri(address) return display_name, uri, rest if not display_name: address_match = NameAddress.NO_DISPLAY_NAME_RX.match(rest) if address_match: address, rest = address_match.group(1).strip(), address_match.group(2) uri = Uri(address) return display_name, uri, rest else: uri = Uri(rest) return display_name, uri, '' except NameAddressError as e: raise NameAddressError(f'Cannot parse {string}: {e}') except Exception as e: raise NameAddressError(f'Error occurred while parsing {string}: {e}')
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_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_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_process_route_info_no_rr_checker(): 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' sip_msg = process_route_info(raw_message(msg), ValidationOptions()) sip_msg = rebuild_sip_msg(sip_msg) assert sip_msg.ruri == Uri(bob_uri) assert sip_msg.get(ROUTE_HEADER) == RouteHeader.make(f'<{this_proxy_uri}>,<{next_proxy_uri}>')
def test_params_transport_tcp(): uri_str = b'sip:b;transport=tcp' uri = Uri(uri_str) assert 'b' == uri.host assert {PARAM_TRANSPORT: 'tcp'} == uri.params
def test_invalid_hostport(uri_str): with pytest.raises(PySIPException): Uri(uri_str)
def test_uri_simple_parse(): uri_str = b'sip:a@b:5090' uri = Uri(uri_str) assert ('a', 'b', 5090) == (uri.user, uri.host, uri.port)
def test_host_only(): uri_str = b'sip:b' uri = Uri(uri_str) assert 'b' == uri.host
def test_empty_user(): uri_str = b'sip:b:5090' uri = Uri(uri_str) assert (None, 'b', 5090) == (uri.user, uri.host, uri.port)
def test_invalid_username(uri_str): with pytest.raises(UserParseError): Uri(uri_str)
def test_invalid_scheme(): uri_str = b"?:a@b:5090" with pytest.raises(URIParseError): Uri(uri_str)
def test_percent_sign_user(): uri_str = b"sips:%20@b:5090" uri = Uri(uri_str) assert ('sips', '%20', 'b', 5090) == (uri.scheme, uri.user, uri.host, uri.port)