def test_set_rport_to_true(): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;rport=1234;maddr=x.com' ) via.set_param(PARAM_RPORT, True) assert via.assemble( ) == 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;rport;maddr=x.com' assert via.rport is True
def add_via(self, message, branch): via_hdr = message.get(VIA_HEADER) via = ViaHeader() via.sent_by = SentBy(host=self.local_addr, port=self.local_port) via.sent_protocol = SentProtocol(transport=self.transport, version='2.0', name='SIP') via.branch = branch via_hdr.add_topmost(via) message.set_header(via_hdr)
def test_via_params(): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=z9hG4bK77ef4c2312983.1;rport;x=1;some' ) assert Branch('z9hG4bK77ef4c2312983.1') == via.branch assert via.rport is True assert via.get_raw_param('x') == '1' assert via.get_raw_param('rport') is None assert via.get_raw_param('some') is None
def check_via_match(self, via): if self.check_via_match_address(via) and self.check_via_match_transport(via): return ViaMatchResult(result=ViaMatchResult.MATCH) else: expected = ViaHeader() expected.sent_by = SentBy(host=self.local_addr, port=self.local_port) expected.sent_protocol = SentProtocol(transport=self.transport, version='2.0', name='SIP') expected_str = expected.assemble() actual_str = via.assemble() return ViaMatchResult(result=ViaMatchResult.MISMATCH, expected=expected_str, actual=actual_str)
def test_set_param_received_ipv6(): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com' ) via.set_param(PARAM_RECEIVED, '[::1]') assert via.assemble( ) == 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=[::1];maddr=x.com' assert via.received == ipaddress.IPv6Address('::1') via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com' ) via.set_param(PARAM_RECEIVED, ipaddress.IPv6Address('::1')) assert via.assemble( ) == 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=[::1];maddr=x.com' assert via.received == ipaddress.IPv6Address('::1')
def test_via_branch(): branch_value = 'z9hG4bK776asdhds' branch = Branch(branch_value) via = ViaHeader('SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=' + branch_value) assert via.branch == branch assert via.branch.make_key() == branch.make_key()
def check_received(remote_ip, msg, conn): x, new_request = conn.connection_data(msg) assert isinstance(new_request, NewRequest) new_msg = new_request.message via_hdr = new_msg.get('via') via = ViaHeader.topmost_via(via_hdr) assert remote_ip == via.received
def test_via_params_ipv6(): hdr = Header('Via') hdr.add_value( "SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=[::1];maddr=[::1]" ) via = ViaHeader.topmost_via(hdr) assert via.received == ipaddress.IPv6Address('::1') assert via.maddr == ipaddress.IPv6Address('::1')
def test_set_param_received_ipv4(): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com' ) via.set_param(PARAM_RECEIVED, '2.2.2.2') assert via.received == ipaddress.IPv4Address('2.2.2.2') via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com' ) via.set_param(PARAM_RECEIVED, ipaddress.IPv4Address('2.2.2.2')) assert via.received == ipaddress.IPv4Address('2.2.2.2')
def test_via_params_2(): hdr = Header('Via') hdr.add_value( "SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com" ) via = ViaHeader.topmost_via(hdr) assert via.branch == Branch('branch_v') assert via.ttl == 200 assert via.received == ipaddress.IPv4Address('1.1.1.1') assert via.maddr == 'x.com'
def test_topmost_via(): hdr = Header('Via') hdr.add_values([ "SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1", "SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds" ]) via = ViaHeader.topmost_via(hdr) assert SentProtocol('SIP', '2.0', Transport('UDP')) == via.sent_protocol assert 'bigbox3.site3.atlanta.com' == via.sent_by.host assert 5060 == via.sent_by.port
def test_topmost_via_ipport(): hdr = Header('via') hdr.add_values([ "SIP/2.0/TCP 192.168.1.1:5090;branch=z9hG4bK77ef4c2312983.1", "SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1", "SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds" ]) via = ViaHeader.topmost_via(hdr) assert SentProtocol('SIP', '2.0', Transport('TCP')) == via.sent_protocol assert ipaddress.IPv4Address('192.168.1.1') == via.sent_by.host assert 5090 == via.sent_by.port
def receive_request_process_via(self, msg): print(f'StatelessProxy.receive_request_process_via()') via_hdr = msg.get(VIA_HEADER) print(f'StatelessProxy.receive_request_process_via(): via header {via_hdr}') via = ViaHeader.topmost_via(via_hdr) print(f'StatelessProxy.receive_request_process_via(): topmost via {via}') self.maybe_add_received(via) print(f'StatelessProxy.receive_request_process_via(): via with received {via}') self.maybe_fill_rport(via) print(f'StatelessProxy.receive_request_process_via(): via with rport {via}') via_hdr.replace_topmost(via.assemble()) print(f'StatelessProxy.receive_request_process_via(): via with replaced topmost {via_hdr}') msg.set_header(via_hdr)
def test_topmost_via_with_spaces(): hdr = Header('Via') hdr.add_value( 'SIP / 2.0 / UDP first.example.com: 4000;ttl=16 ;maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1' ) via = ViaHeader.topmost_via(hdr) assert via.sent_protocol.name == 'SIP' assert via.sent_protocol.version == '2.0' assert via.sent_protocol.transport == Transport('UDP') assert via.branch == Branch('z9hG4bKa7c6a8dlze.1') assert via.maddr == ipaddress.IPv4Address('224.2.0.1') assert via.ttl == 16 assert via.sent_by.host == 'first.example.com' assert via.sent_by.port == 4000
def take_via(self, message): via_hdr = message.get(VIA_HEADER) try: value = via_hdr.take_topmost() try: via = ViaHeader.parse(value) via_match = self.check_via_match(via) if via_match: message.set_header(via_hdr) else: raise SipConnectionError(f'Via mismatch: expected {via_match.expected}, got {via_match.actual}') except Exception as e: raise SipConnectionError(f'Bad via: {e}') except NoHeaderError: raise SipConnectionError(f'No via.')
def test_ne(via_str1, via_str2): via1 = ViaHeader(via_str1) via2 = ViaHeader(via_str2) assert via1 != via2
def test_set_rport_error(rport): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;rport=4321;maddr=x.com' ) with pytest.raises(ViaHeaderError): via.set_param(PARAM_RPORT, rport)
def test_set_received_error(rec_str): via = ViaHeader( 'SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;ttl=200;received=1.1.1.1;maddr=x.com' ) with pytest.raises(ViaHeaderError): via.set_param(PARAM_RECEIVED, rec_str)
def test_parse_fail(via_str): with pytest.raises(ViaHeaderError): ViaHeader(via_str)
def test_eq(via_str1, via_str2): via1 = ViaHeader(via_str1) via2 = ViaHeader(via_str2) assert via1 == via2
def check_no_received(msg, conn): x, new_request = conn.connection_data(msg) via_hdr = new_request.message.get('via') via = ViaHeader.topmost_via(via_hdr) assert via.received is None
def test_via_gen_params(): hdr = Header('Via') hdr.add_value('SIP/2.0/TCP 192.168.1.1:5090;branch=branch_v;my_param=abc') via = ViaHeader.topmost_via(hdr) assert via.get_raw_param('my_param') == 'abc'
def test_sent_by_key(): via1 = ViaHeader('SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=x') via2 = ViaHeader('SIP/2.0/UDP BIGBOX3.SITE3.ATLANTA.COM;branch=y') assert via1.sent_by == via2.sent_by
def test_assemble(via_str): via1 = ViaHeader(via_str) hdr = Header('Via') hdr.add_value(via1.assemble()) via2 = ViaHeader.topmost_via(hdr) assert via1 == via2
def check_rport(remote_port, msg, conn): x, new_request = conn.connection_data(msg) via_hdr = new_request.message.get('via') via = ViaHeader.topmost_via(via_hdr) assert remote_port == via.rport