예제 #1
0
    def test_unsigned_integers(self):
        """
        Tests we can encode and decode unsigned integers
        """

        self._compare_avp(
            avp.Unsigned32AVP(299, 1234),
            memoryview(b'\x00\x00\x01+\x00\x00\x00\x0c\x00\x00\x04\xd2'))

        with self.assertRaises(CodecException):
            avp.Unsigned32AVP(299, -1234)
예제 #2
0
    def test_addresses(self):
        """
        Tests we can encode and decode addresses and handle invalid payloads
        """
        # pylint:disable=expression-not-assigned

        self._compare_avp(
            avp.AddressAVP(257, '127.0.0.1'),
            memoryview(b'\x00\x00\x01\x01\x00\x00\x00\x0e'
                       b'\x00\x01\x7f\x00\x00\x01\x00\x00'))

        self._compare_avp(
            avp.AddressAVP(257, '2001:db8::1'),
            memoryview(b'\x00\x00\x01\x01\x00\x00\x00\x1a\x00\x02 \x01\r'
                       b'\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x01\x00\x00'))

        # Can't read invalid address type \x03
        with self.assertRaises(CodecException):
            avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e'
                       b'\x00\x03\x7f\x00\x00\x01\x00\x00').value

        # Can't read too short IPV4
        with self.assertRaises(CodecException):
            avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e'
                       b'\x00\x01\x7f').value

        # Can't read too short IPV6
        with self.assertRaises(CodecException):
            avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e'
                       b'\x00\x02\x7f\x00\x00\x01\x00\x00').value

        # Cant encode non-ips
        with self.assertRaises(CodecException):
            avp.Unsigned32AVP(257, 'facebook.com')