Beispiel #1
0
    def _test_pack_unpack(self, *args, **kwargs):
        """Pack the message, unpack and check whether they are the same.

        Call this method multiple times if you want to test more than one
        object.
        """
        obj = self._msg_cls(*args, **kwargs)
        packed = obj.pack()
        header = Header()
        header_size = header.get_size()
        header.unpack(packed[:header_size])
        unpacked = new_message_from_header(header)
        unpacked.unpack(packed[header_size:])

        self.assertEqual(packed, unpacked.pack())
Beispiel #2
0
def unpack_message(buffer):
    """Unpack the whole buffer, including header pack.

    Args:
        buffer (bytes): Bytes representation of a openflow message.

    Returns:
        object: Instance of openflow message.

    """
    hdr_size = Header().get_size()
    hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:]
    header = Header()
    header.unpack(hdr_buff)
    message = new_message_from_header(header)
    message.unpack(msg_buff)
    return message
Beispiel #3
0
class TestHeader(unittest.TestCase):
    """Test the message Header."""
    def setUp(self):
        """Setup the TestHeader Class instantiating a HELLO header."""
        self.message = Header()
        self.message.message_type = Type.OFPT_HELLO
        self.message.xid = 1
        self.message.length = 0

    def test_size(self):
        """[Common/Header] - size 8."""
        self.assertEqual(self.message.get_size(), 8)

    @unittest.expectedFailure
    def test_pack_empty(self):
        """[Common/Header] - packing empty header."""
        self.assertRaises(TypeError, Header().pack())

    def test_pack(self):
        """[Common/Header] - packing Hello."""
        packed_header = b'\x04\x00\x00\x00\x00\x00\x00\x01'
        self.assertEqual(self.message.pack(), packed_header)

    def test_unpack(self):
        """[Common/Header] - unpacking Hello."""
        filename = os.path.join(os.path.dirname(os.path.realpath('__file__')),
                                'raw/v0x05/ofpt_hello.dat')
        try:
            f = open(filename, 'rb')
            self.message.unpack(f.read(8))

            self.assertEqual(self.message.length, 8)
            self.assertEqual(self.message.xid, 1)
            self.assertEqual(self.message.message_type, Type.OFPT_HELLO)
            self.assertEqual(self.message.version, 0x04)

            f.close()
        except FileNotFoundError:
            raise self.skipTest('There is no raw dump file for this test')