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_stateless_proxy_forward_to_proxy(): def check_rroute_fun(uri): return uri == Uri(this_proxy_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' 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(next_proxy_uri) sip_msg, opts = CommonProxy.forward_request(target, sip_msg, proxy_opts) expected_route_hdr = RouteHeader(f'<{next_proxy_uri}>') assert target == opts.nexthop assert expected_route_hdr == sip_msg.find(ROUTE_HEADER)
def test_first_and_last(): rs = RouteSet() with pytest.raises(RouteSetError): l = rs.last with pytest.raises(RouteSetError): f = rs.first r1 = RouteHeader('<sip:a@b>') rs.add_first(r1) assert r1 == rs.first assert r1 == rs.last r2 = RouteHeader('<sip:b@a>') rs.add_first(r2) assert r1 == rs.last assert r2 == rs.first rs.remove_first() assert r1 == rs.first assert r1 == rs.last rs.remove_first() with pytest.raises(RouteSetError): l = rs.last with pytest.raises(RouteSetError): f = rs.first
def _strict_router_workaround(fwd_result): # The proxy MUST place the Request-URI into the Route header # field as the last value. sip_msg, opts = fwd_result route_hdr = sip_msg.get(ROUTE_HEADER) ruri_route = RouteHeader.make_route(sip_msg.ruri) route_hdr.route_set.add_last(ruri_route) # The proxy MUST then place the first Route header field value # into the Request-URI and remove that value from the Route # header field. sip_msg.ruri = route_hdr.route_set.first.uri route_hdr.route_set.remove_first() sip_msg.set(ROUTE_HEADER, route_hdr) return sip_msg, ForwardOptions(routing='strict')
def _fwd_record_route(target, fwd_result, proxy_params): """ Args: target: fwd_result: proxy_params (ProxyOptionsDetails): Returns: """ sip_msg, opts = fwd_result if proxy_params.record_route_uri is not None: rr = proxy_params.record_route_uri.clear_not_allowed_parts( RECORD_ROUTE_HEADER) rr.set_param(PARAM_LR, True) rrroute = RouteHeader.make_route(rr) rr_set = sip_msg.find(RECORD_ROUTE_HEADER) if isinstance(rr_set, NotFound): rr_set = RouteHeader() rr_set.route_set.add_first(rrroute) sip_msg.set(RECORD_ROUTE_HEADER, rr_set) return sip_msg, 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_is_empty(): rs = RouteSet() assert rs.is_empty() route = RouteHeader('<sip:a@b>') rs.add_first(route) assert not rs.is_empty()
def test_topmost_route(): hdr = Header('Route') hdr.add_value('<sip:[email protected]>,<sip:[email protected]>,<sip:[email protected]>') route_hdr = RouteHeader(hdr) uri = Uri('sip:[email protected]') assert route_hdr.first.uri == uri
def test_parse_error(val): hdr = Header('Route') hdr.add_value(val) with pytest.raises(RouteHeaderError): RouteHeader(hdr)
def test_empty_route_set(): hdr = Header('Route') route_hdr = RouteHeader(hdr) assert route_hdr.route_set.is_empty()
def test_invalid_lr(): hdr = Header('Route') hdr.add_value('<sip:[email protected]>;lr') route_hdr = RouteHeader(hdr) assert not route_hdr.first.uri.params
def test_bad_middle(): hdr = Header('Route') hdr.add_value('<sip:[email protected]>,?,<sip:[email protected]>') with pytest.raises(RouteHeaderError): RouteHeader(hdr)
def test_parse_route_error(): with pytest.raises(RouteHeaderError): RouteHeader.parse_route('?')
def test_topmost_route_with_lr(): hdr = Header('Route') hdr.add_value('<sip:[email protected];lr>') route_hdr = RouteHeader(hdr) assert route_hdr.first.uri.params == {'lr': True}
def test_topmost_route_with_ext(): hdr_with_ext = Header('Route') hdr_with_ext.add_value('<sip:[email protected]>;extension=1') route_hdr_with_ext = RouteHeader(hdr_with_ext) assert [('extension', '1')] == route_hdr_with_ext.first.params