Example #1
0
def test_reassemble(ft_str):
    ft_hdr = FromToHeader(ft_str)
    hdr = Header('To')
    hdr.add_value(ft_hdr.assemble())
    ft_hdr2 = FromToHeader(hdr)
    print(f'{ft_hdr} == {ft_hdr2}')
    assert ft_hdr == ft_hdr2
Example #2
0
def test_build():
    h = Header("CSeq")
    h.add_value('314159 INVITE')
    hvalues = ''.join(h.values)
    cseq = CSeqHeader(header=h)
    cseq_values = ''.join(cseq.build().values)
    assert hvalues == cseq_values
Example #3
0
def test_build():
    header = Header('Content-Type')
    header.add_value("application/sdp")
    ct_hdr = ContentTypeHeader(header=header)
    hvalues = ''.join(header.values)
    ct_hvalues = ''.join(ct_hdr.build().values)
    assert hvalues == ct_hvalues
Example #4
0
def test_parse(maxforwards, expected):
    mf_hdr = MaxForwardsHeader(maxforwards)
    hdr = Header('Max-Forwards')
    hdr.add_value(maxforwards)
    mf_hdr_2 = MaxForwardsHeader(hdr)
    assert mf_hdr == mf_hdr_2
    assert mf_hdr_2.maxforwards == mf_hdr.maxforwards == expected
Example #5
0
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')
Example #6
0
def test_no_split_authenticate():
    h = Header(b'WWW-Authenticate')
    h.add_value(
        b'"Digest realm="atlanta.com", domain="sip:boxesbybob.com", qop="auth", '
        b'nonce="f84f1cec41e6cbe5aea9c8e88d359", opaque="", stale=FALSE, algorithm=MD5"'
    )
    assert b'"Digest realm="atlanta.com", domain="sip:boxesbybob.com", qop="auth", ' \
           b'nonce="f84f1cec41e6cbe5aea9c8e88d359", opaque="", stale=FALSE, algorithm=MD5"' == to_bytes(h.values[0])
Example #7
0
def test_no_split_authorization():
    h = Header(b'Authorization')
    h.add_value(b'Digest username="******", realm="atlanta.com",'
                b' nonce="84a4cc6f3082121f32b42a2187831a9e",'
                b' response="7587245234b3434cc3412213e5f113a5432"')
    assert b'Digest username="******", realm="atlanta.com",' \
           b' nonce="84a4cc6f3082121f32b42a2187831a9e",' \
           b' response="7587245234b3434cc3412213e5f113a5432"' == to_bytes(h.values[0])
Example #8
0
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'
Example #9
0
def test_no_split_proxy_authenticate():
    h = Header(b'Proxy-Authenticate')
    h.add_value(b'Digest realm="atlanta.com",'
                b' domain="sip:ss1.carrier.com", qop="auth",'
                b' nonce="f84f1cec41e6cbe5aea9c8e88d359",'
                b' opaque="", stale=FALSE, algorithm=MD5')
    assert b'Digest realm="atlanta.com",' \
           b' domain="sip:ss1.carrier.com", qop="auth",' \
           b' nonce="f84f1cec41e6cbe5aea9c8e88d359",' \
           b' opaque="", stale=FALSE, algorithm=MD5' == to_bytes(h.values[0])
Example #10
0
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
Example #11
0
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
Example #12
0
    def make(route):
        """Makes route header out of route string.

        Args:
            route (str):

        Returns:
            :obj:RouteHeader - RouteHeader instance.
        """
        h = Header('Route')
        h.add_value(route)
        return RouteHeader.parse(h)
Example #13
0
 def set_header(header_name, header_value, msg):
     descr = SipHeader.header_descr(header_name)
     print_name = PRINT_FORM_MAP.get(make_key(header_name), header_name)
     raw_hdr = Header(print_name)
     if header_value.assemble():
         raw_hdr.add_value(header_value.assemble())
     if not raw_hdr.values:
         msg.headers.pop(make_key(header_name), None)
         msg.raw_message.delete_header(make_key(header_name))
     else:
         msg.headers[make_key(header_name)] = header_value
         msg.raw_message.set_header(raw_hdr)
Example #14
0
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
Example #15
0
 def get(self, item):
     if item == ITEM_TYPE:
         return self._type.name
     elif item == ITEM_STATUS:
         if not isinstance(self._type, ResponseType):
             raise MessageError(
                 f'Cannot get message status for {self}: message should be of type ResponseType'
             )
         return self._type.status
     elif item == ITEM_REASON:
         if not isinstance(self._type, ResponseType):
             raise MessageError(
                 f'Cannot get message reason for {self}: message should be of type ResponseType'
             )
         return self._type.reason
     elif item == ITEM_METHOD:
         if not isinstance(self._type, RequestType):
             raise MessageError(
                 f'Cannot get message method for {self}: message should be of type RequestType'
             )
         return self._type.method
     elif item == ITEM_RURI:
         if not isinstance(self._type, RequestType):
             raise MessageError(
                 f'Cannot get message ruri for {self}: message should be of type RequestType'
             )
         return self._type.ruri
     elif item == ITEM_BODY:
         return self.body
     else:
         hdr = self._headers.get(make_key(item), Header(item))
         return hdr
Example #16
0
 def add(self, name, value):
     if not make_key(name) in self._headers:
         self._headers[make_key(name)] = Header(name)
     if not isinstance(value, str):
         raise MessageError(
             f'Cannot add header {name}={value} to message {self.serialize()}: value should be '
             f'string not ({type(value)}).')
     self._headers[make_key(name)].add_value(value.lstrip())
Example #17
0
 def build(self, header_name):
     hdr = Header(header_name)
     if self.is_star():
         hdr.add_value(STAR)
     else:
         for contact in self.contact_list:
             hdr.add_value(contact.assemble())
     return hdr
Example #18
0
    def add(self, headername, value):
        """Adds value to header.

        Args:
            headername (str or HeaderKey): header name.
            value (str): header value.
        """
        if not make_key(headername) in self.headers:
            self.headers[make_key(headername)] = Header(headername)
        if not isinstance(value, str):
            raise SipMessageError(
                f'Cannot add header {headername}={value} to message {self.serialize()}: value should be '
                f'string not ({type(value)}).')
        self.headers[make_key(headername)].add_value(value.lstrip())
Example #19
0
def test_request_validation_maxforwards_is_zero_options_reply_no_allow():
    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'
    options = ValidationOptions(validate=ValidationOptionsDetails(reply_on_options=True))
    response = CommonProxy.validate_request(raw_message(msg), options)
    assert response.status == 200
    resp_raw_message = raw_message(response.serialize())
    assert resp_raw_message.get(ALLOW_HEADER) == Header(ALLOW_HEADER)
Example #20
0
def test_serialize_multi_values():
    hdr = Header(b'Via')
    hdr.add_value(
        b'SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1')
    hdr.add_value(b'SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds')
    assert b'Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' == hdr.serialize_to_bytes(
    )
    assert 'Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' == hdr.serialize_to_string(
    )
Example #21
0
 def set(self, name, value):
     if name == ITEM_TYPE:
         self.type = value
     elif name == ITEM_BODY:
         self.body = value
     elif name == ITEM_REASON:
         self.reason = value
     elif name == ITEM_STATUS:
         self.status = value
     elif name == ITEM_METHOD:
         self.method = value
     elif name == ITEM_RURI:
         self.ruri = value
     else:
         key = make_key(name)
         hdr = Header(name)
         if isinstance(value, list):
             for v in value:
                 hdr.add_value(v)
         elif isinstance(value, str):
             hdr.add_value(value)
         self._headers[key] = hdr
Example #22
0
def test_allow_empty():
    h = Header(b'Allow')
    h.add_values([])
    assert b'A: B' == h.serialize_to_bytes(append_value=b'A: B')
Example #23
0
def test_parse_invite():
    hdr = Header('CSeq')
    hdr.add_value('314159 INVITE')
    cseq = CSeqHeader(header=hdr)
    assert cseq.number == 314159
    assert cseq.method == INVITE
Example #24
0
def test_as_integer(header_name, header_val, expected):
    header = Header(header_name)
    header.add_value(header_val)
    assert expected == header.as_integer()
Example #25
0
def test_parse_error(value):
    hdr = Header('CSeq')
    hdr.add_value(value)
    with pytest.raises(PySIPException):
        CSeqHeader(header=hdr)
Example #26
0
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'
Example #27
0
def test_no_value():
    hdr = Header('CSeq')
    with pytest.raises(CSeqHeaderError):
        CSeqHeader(header=hdr)
Example #28
0
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
Example #29
0
 def build(self, header_name='CSeq'):
     hdr = Header(header_name)
     hdr.add_value(self.assemble())
     return hdr
Example #30
0
def test_parse_register():
    hdr = Header('CSeq')
    hdr.add_value('1 REGISTER')
    cseq = CSeqHeader(header=hdr)
    assert cseq.number == 1
    assert cseq.method == REGISTER