Ejemplo n.º 1
0
 def _from_gpb_value(self, value):
     """Translates value from GPB's representation into the internal
     representation suitable for encoding."""
     if self.format.format_str == DataDescription.MAC_FMT:
         return Convert.mac_to_tuple_of_bytes(value)
     elif self.format.format_str == DataDescription.IP_FMT:
         return Convert.ipaddr_to_tuple_of_bytes(value)
     elif isinstance(value, unicode):
         return str(value)
     return value
Ejemplo n.º 2
0
 def test_MAC_negative(self):
     self.assertIsNone(Convert.mac_to_tuple_of_bytes(1))
     self.assertIsNone(Convert.mac_to_tuple_of_bytes(""))
     self.assertIsNone(Convert.mac_to_tuple_of_bytes("11:"))
     self.assertIsNone(Convert.mac_to_tuple_of_bytes("01:02:03:04:05"))
     self.assertIsNone(Convert.mac_to_tuple_of_bytes("GG:02:03:04:05:06"))
     self.assertIsNone(Convert.mac_to_tuple_of_bytes("125:02:03:04:05"))
Ejemplo n.º 3
0
 def _is_valid(self, value, name):
     try:
         fmt = self.rcptlv.child_dict_by_name[name]
         if fmt.value_is_mac():
             value = Convert.mac_to_tuple_of_bytes(value)
         elif fmt.value_is_ip_addr():
             value = Convert.ipaddr_to_tuple_of_bytes(value)
         fmt.validate(value)
         return True
     except GCPInvalidDataValueError:
         return False
     except ValueError:
         return False
     except TypeError:
         return False
Ejemplo n.º 4
0
    def test_bytes_convert(self):
        illegal_var = '01:02:03:192.168.0.1:0a:0b:'
        try:
            Convert.bytes_to_mac_str(illegal_var)
        except TypeError:
            pass
        else:
            self.fail('illegal mac been converted')

        try:
            Convert.bytes_to_ipv4_str(illegal_var)
        except TypeError:
            pass
        else:
            self.fail('illegal ipv4 been converted')

        try:
            Convert.bytes_to_ipv6_str(illegal_var)
        except TypeError:
            pass
        else:
            self.fail('illegal ipv6 been converted')

        try:
            Convert.bytes_to_ip_addr(illegal_var)
        except TypeError:
            pass
        else:
            self.fail('illegal ip been converted')

        # try:
        ret = Convert.mac_to_tuple_of_bytes('-1:-2:-3:-4:-5')
        # except ValueError:
        #    pass
        # else:
        #    self.fail('illegal mac been converted: %s' % ret)
        self.assertIsNone(ret, 'illegal mac been converted: %s' % ret)

        self.assertFalse(Convert.is_valid_ipv4_address('a.b.c.d'))
Ejemplo n.º 5
0
 def test_MAC_positive(self):
     mac_str = "01:02:03:04:05:06"
     mac_tuple = (1, 2, 3, 4, 5, 6)
     self.assertEqual(Convert.mac_to_tuple_of_bytes(mac_str), mac_tuple)