Example #1
0
    def testFromString(self):
        self.assertEqual(None, MACAddress.FromString(''))
        self.assertEqual(None, MACAddress.FromString('abc'))
        self.assertEqual(None, MACAddress.FromString(':'))
        self.assertEqual(None, MACAddress.FromString('0:1:2'))
        self.assertEqual(None, MACAddress.FromString('12345:1234'))

        mac = MACAddress.FromString('01:23:45:67:89:ab')
        self.assertTrue(mac)
        self.assertEqual(b'\x01\x23\x45\x67\x89\xab', bytes(mac.mac_address))
        self.assertEqual('01:23:45:67:89:ab', str(mac))

        mac2 = MACAddress.FromString('98.76.54.fe.dc.ba')
        self.assertTrue(mac2)
        self.assertEqual(b'\x98\x76\x54\xfe\xdc\xba', bytes(mac2.mac_address))
        self.assertEqual('98:76:54:fe:dc:ba', str(mac2))
Example #2
0
    def Pack(self, args):
        mac = None
        if isinstance(args[0], MACAddress):
            mac = args[0]
        else:
            mac = MACAddress.FromString(args[0])

        if mac is None:
            raise ArgsValidationError("Invalid MAC Address: %s" % e)

        format_string = self._FormatString()
        try:
            data = struct.pack(format_string, mac.mac_address[0],
                               mac.mac_address[1], mac.mac_address[2],
                               mac.mac_address[3], mac.mac_address[4],
                               mac.mac_address[5])
        except struct.error, e:
            raise ArgsValidationError("Can't pack data: %s" % e)