Example #1
0
    def convert_message_to_older_version(self, convert_to_version: int,
                                         msg: AbstractBloxrouteMessage):
        """
        Converts message from current version to provided version

        :param convert_to_version: version to convert to
        :param msg: message
        :return: converted message
        """
        if not convert_to_version:
            raise ValueError("convert_to_version is required")

        if not msg:
            raise ValueError("msg is required")

        if convert_to_version not in self.protocol_to_converter_factory_mapping:
            raise ValueError(
                "Conversion for version {} is not supported".format(
                    convert_to_version))

        if isinstance(
                msg,
                TxMessage) and convert_to_version in msg.converted_message:
            converted_msg = msg.converted_message[convert_to_version]
        else:
            msg_converter = self._get_message_converter(
                convert_to_version, msg.msg_type())
            converted_msg = msg_converter.convert_to_older_version(msg)
            msg.converted_message[convert_to_version] = converted_msg
        return converted_msg
Example #2
0
    def test_ping_pong(self):
        hello_msg = HelloMessage(protocol_version=protocol_version.PROTOCOL_VERSION, network_num=1)
        self.connection.add_received_bytes(hello_msg.rawbytes())
        self.connection.process_message()

        hello_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(hello_msg_bytes) > 0)
        self.connection.advance_sent_bytes(len(hello_msg_bytes))

        ack_msg = AckMessage()
        self.connection.add_received_bytes(ack_msg.rawbytes())
        self.connection.process_message()

        ack_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(ack_msg_bytes) > 0)
        self.connection.advance_sent_bytes(len(ack_msg_bytes))

        ping_msg = PingMessage(nonce=12345)
        self.connection.add_received_bytes(ping_msg.rawbytes())
        self.connection.process_message()

        pong_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(pong_msg_bytes) > 0)

        msg_type, payload_len = AbstractBloxrouteMessage.unpack(pong_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH])
        self.assertEqual(BloxrouteMessageType.PONG, msg_type)
        self.connection.advance_sent_bytes(len(pong_msg_bytes))

        time.time = MagicMock(return_value=time.time() + constants.PING_INTERVAL_S)
        self.node.alarm_queue.fire_alarms()

        ping_msg_bytes = self.connection.get_bytes_to_send()
        self.assertTrue(len(ping_msg_bytes) > 0)
        msg_type, payload_len = AbstractBloxrouteMessage.unpack(ping_msg_bytes[:AbstractBloxrouteMessage.HEADER_LENGTH])
        self.assertEqual(BloxrouteMessageType.PING, msg_type)
Example #3
0
 def setUp(self):
     self.buf1 = bytearray([i for i in range(40)])
     self.payload_len1 = 20
     self.msg_type1 = b"example"
     self.message1 = AbstractBloxrouteMessage(msg_type=self.msg_type1,
                                              payload_len=self.payload_len1,
                                              buf=self.buf1)
     self.buf2 = bytearray([i for i in range(24)])
     self.payload_len2 = 50
     self.msg_type2 = b"hello"
     self.message2 = AbstractBloxrouteMessage(msg_type=self.msg_type2,
                                              payload_len=self.payload_len2,
                                              buf=self.buf2)
Example #4
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new message type to v6: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type]
        old_version_payload_len = (msg.payload_len() -
                                   constants.TRANSACTION_FLAG_LEN -
                                   constants.DOUBLE_SIZE_IN_BYTES)

        old_version_msg_bytes = bytearray(
            self._LEFT_BREAKPOINT +
            len(msg.rawbytes()[self._RIGHT_BREAKPOINT:]))
        old_version_msg_bytes[:self._LEFT_BREAKPOINT] = msg.rawbytes(
        )[:self._LEFT_BREAKPOINT]
        old_version_msg_bytes[self._LEFT_BREAKPOINT:] = msg.rawbytes(
        )[self._RIGHT_BREAKPOINT:]

        struct.pack_into(
            "<12sL",
            old_version_msg_bytes,
            AbstractBloxrouteMessage.STARTING_BYTES_LEN,
            msg_type,
            old_version_payload_len,
        )
        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to "
                f"v7: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = (
            msg.payload_len() +
            constants.QUOTA_FLAG_LEN +
            constants.UL_INT_SIZE_IN_BYTES
        )

        new_msg_bytes = bytearray(
            AbstractBloxrouteMessage.HEADER_LENGTH + new_payload_len
        )
        new_msg_bytes[: self._LEFT_BREAKPOINT] = msg.rawbytes()[
            : self._LEFT_BREAKPOINT
        ]

        struct.pack_into("<B", new_msg_bytes, self._LEFT_BREAKPOINT, 0)
        struct.pack_into("<L", new_msg_bytes, self._LEFT_BREAKPOINT, 0)
        new_msg_bytes[self._RIGHT_BREAKPOINT :] = msg.rawbytes()[
            self._LEFT_BREAKPOINT :
        ]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len)
        )
Example #6
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to v9: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.BROADCAST_TYPE_LEN

        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:self._BASE_LENGTH] = msg.rawbytes()[:self._BASE_LENGTH]
        struct.pack_into(
            "<4s",
            new_msg_bytes,
            self._BASE_LENGTH,
            # pylint: disable=no-member
            BroadcastMessageType.BLOCK.value.encode(
                constants.DEFAULT_TEXT_ENCODING))
        new_msg_bytes[self._BREAKPOINT:] = msg.rawbytes()[self._BASE_LENGTH:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Example #7
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new message type to v8: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type]
        old_version_payload_len = msg.payload_len(
        ) - constants.BROADCAST_TYPE_LEN

        old_version_msg_bytes = bytearray(
            self._BASE_LENGTH + len(msg.rawbytes()[self._BREAKPOINT:]))
        old_version_msg_bytes[:self._BASE_LENGTH] = msg.rawbytes(
        )[:self._BASE_LENGTH]
        old_version_msg_bytes[self._BASE_LENGTH:] = msg.rawbytes(
        )[self._BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
Example #8
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type to v6: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.NODE_ID_SIZE_IN_BYTES

        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:self._LEFT_BREAKPOINT] = msg.rawbytes(
        )[:self._LEFT_BREAKPOINT]
        new_msg_bytes[self._RIGHT_BREAKPOINT:] = msg.rawbytes(
        )[self._LEFT_BREAKPOINT:]

        # pack empty source id for old gateways, since message converters are singletons
        # and source ids for gateway don't matter anyway
        struct.pack_into("<16s", new_msg_bytes, self._LEFT_BREAKPOINT,
                         uuid_pack.to_bytes(""))

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
    def compare_old_to_current(
        self,
        converted_current_message: AbstractBloxrouteMessage,
        original_current_message: AbstractBloxrouteMessage,
    ):
        """
        This method is run on every message comparision, when comparing
        the old version converted to the current version.

        Override this if a change is made that affects every message.
        """
        self.assertEqual(
            constants.STARTING_SEQUENCE_BYTES,
            converted_current_message.rawbytes()
            [:constants.STARTING_SEQUENCE_BYTES_LEN],
        )
        self.assertEqual(
            original_current_message.msg_type(),
            converted_current_message.msg_type(),
        )
        self.assertEqual(
            original_current_message.payload_len(),
            converted_current_message.payload_len(),
        )
        self.assertEqual(
            original_current_message.get_control_flags(),
            converted_current_message.get_control_flags(),
        )
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v16: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + self._LENGTH_DIFFERENCE

        new_msg_bytes = bytearray(self._NEW_MESSAGE_LEN)
        new_msg_bytes[:self._INTERVAL_TIMES_BREAKPOINT] = msg.rawbytes()[:self._INTERVAL_TIMES_BREAKPOINT]
        off = self._BASE_LENGTH + self._INTERVAL_TIMES_LENGTH

        new_msg_bytes[off:off + self._MEMORY_UTILIZATION_LENGTH] = \
            msg.rawbytes()[
                self._FIRST_STATS_SETS_BREAKPOINT:self._FIRST_STATS_SETS_BREAKPOINT + self._MEMORY_UTILIZATION_LENGTH
            ]
        off += self._MEMORY_UTILIZATION_LENGTH

        # single blockchain peer
        struct.pack_into("<H", new_msg_bytes, off, 1)
        off += constants.UL_SHORT_SIZE_IN_BYTES

        # placeholder ip/port
        message_utils.pack_ip_port(new_msg_bytes, off, "0.0.0.0", 0)
        off += constants.IP_ADDR_SIZE_IN_BYTES + constants.UL_SHORT_SIZE_IN_BYTES

        new_msg_bytes[off:off + self._FIRST_STATS_SETS_LENGTH] = \
            msg.rawbytes()[
                self._INTERVAL_TIMES_BREAKPOINT:self._INTERVAL_TIMES_BREAKPOINT + self._FIRST_STATS_SETS_LENGTH
            ]
        off += self._FIRST_STATS_SETS_LENGTH

        new_msg_bytes[off:off + self._SECOND_STATS_SET_LENGTH] = \
            msg.rawbytes()[
                self._MEMORY_UTILIZATION_BREAKPOINT:self._MEMORY_UTILIZATION_BREAKPOINT + self._SECOND_STATS_SET_LENGTH
            ]
        off += self._SECOND_STATS_SET_LENGTH

        default_new_stats = 0
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES
        struct.pack_into("<I", new_msg_bytes, off, default_new_stats)
        off += constants.UL_INT_SIZE_IN_BYTES

        new_msg_bytes[off:] = msg.rawbytes()[self._SECOND_STATS_SET_BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class,
            new_msg_bytes,
            (msg_type, new_payload_len)
        )
 def test_broadcast_message_cut_through_from_old(self):
     block_hash = Sha256Hash(helpers.generate_bytes(crypto.SHA256_HASH_LEN))
     blob = helpers.generate_bytes(250)
     network_num = 1234
     old_message = BroadcastMessageV5(network_num=network_num,
                                      msg_hash=block_hash,
                                      blob=blob)
     old_message_bytes = old_message.rawbytes()
     new_message_bytes = bloxroute_version_manager.convert_message_first_bytes_from_older_version(
         5, BroadcastMessage.MESSAGE_TYPE, old_message_bytes)
     new_message_bytes = bloxroute_version_manager.convert_message_last_bytes_from_older_version(
         5, BroadcastMessage.MESSAGE_TYPE, new_message_bytes)
     msg_type, payload_length = AbstractBloxrouteMessage.unpack(
         new_message_bytes)
     new_msg = AbstractBloxrouteMessage.initialize_class(
         BroadcastMessage, bytearray(new_message_bytes.tobytes()),
         (msg_type, payload_length))
     self._validate_messages_match(old_message, new_msg)
     self.assertEqual(EMPTY_SOURCE_ID_STR, new_msg.source_id())
Example #12
0
    def test_init(self):
        with self.assertRaises(struct.error):
            AbstractBloxrouteMessage(msg_type=None,
                                     payload_len=20,
                                     buf=bytearray([i for i in range(40)]))
        with self.assertRaises(ValueError):
            AbstractBloxrouteMessage(msg_type=b"hello",
                                     payload_len=-5,
                                     buf=bytearray([i for i in range(40)]))
        with self.assertRaises(ValueError):
            AbstractBloxrouteMessage(msg_type=b"hello",
                                     payload_len=20,
                                     buf=bytearray([i for i in range(10)]))

        self.assertEqual(self.buf1, self.message1.buf)
        self.assertEqual(self.buf1, self.message1._memoryview)
        self.assertEqual(self.msg_type1, self.message1._msg_type)
        self.assertIsNone(self.message1._payload)
        self.assertEqual(self.payload_len1, self.message1._payload_len)
Example #13
0
    def convert_first_bytes_to_older_version(self, first_msg_bytes: memoryview) -> memoryview:
        v5_bytes = broadcast_message_converter_v5.convert_first_bytes_to_older_version(first_msg_bytes)

        command, payload_len = AbstractBloxrouteMessage.unpack(v5_bytes)
        result_bytes = bytearray(len(v5_bytes) - constants.STARTING_SEQUENCE_BYTES_LEN)

        result_bytes[:] = v5_bytes[constants.STARTING_SEQUENCE_BYTES_LEN:]

        struct.pack_into("<12sL", result_bytes, 0, command, payload_len - constants.CONTROL_FLAGS_LEN)

        return memoryview(result_bytes)
Example #14
0
    def test_process_message_quit_on_bad_message(self):
        bad_message = AbstractBloxrouteMessage(
            b"badtype", 0,
            bytearray(constants.STARTING_SEQUENCE_BYTES_LEN +
                      constants.BX_HDR_COMMON_OFF +
                      constants.CONTROL_FLAGS_LEN)).rawbytes()
        self.connection.inputbuf.add_bytes(bad_message)
        self.connection.inputbuf.add_bytes(bad_message)
        self.connection.inputbuf.add_bytes(bad_message)
        self.connection.inputbuf.add_bytes(bad_message)

        self.connection.process_message()
        self.assertFalse(self.connection.is_alive())
    def convert_from_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v18: {msg_type}"
            )
        if len(msg.rawbytes()) == self._MESSAGE_LEN_WITHOUT_NODE_STATS:
            # per Endpoint Stats is not available
            length_difference = 0
            new_message_len = self._MESSAGE_LEN_WITHOUT_NODE_STATS
            num_blockchain_peers = 0
        else:
            num_blockchain_peers, = struct.unpack_from("<I", msg.rawbytes(), self._MEMORY_UTILIZATION_BREAKPOINT)
            length_difference = self._NODE_STATS_LENGTH_DIFFERENCE * num_blockchain_peers
            new_message_len = self._MESSAGE_LEN_WITHOUT_NODE_STATS + \
                (num_blockchain_peers * self._NEW_MESSAGE_NODE_STATS_LEN)

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + length_difference

        new_msg_bytes = bytearray(new_message_len)
        new_msg_bytes[:self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT] = msg.rawbytes()[:self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT]
        new_msg_off = self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT
        old_msg_off = self._NUM_BLOCKCHAIN_PEERS_BREAKPOINT
        default_new_stats = 0
        for _ in range(num_blockchain_peers):
            new_msg_bytes[new_msg_off:new_msg_off + self._OLD_MESSAGE_NODE_STATS_LEN] = \
                msg.rawbytes()[old_msg_off:old_msg_off + self._OLD_MESSAGE_NODE_STATS_LEN]
            new_msg_off += self._OLD_MESSAGE_NODE_STATS_LEN
            old_msg_off += self._OLD_MESSAGE_NODE_STATS_LEN

            struct.pack_into("<I", new_msg_bytes, new_msg_off, default_new_stats)
            new_msg_off += constants.UL_INT_SIZE_IN_BYTES
            struct.pack_into("<I", new_msg_bytes, new_msg_off, default_new_stats)
            new_msg_off += constants.UL_INT_SIZE_IN_BYTES

        new_msg_bytes[new_msg_off:] = msg.rawbytes()[old_msg_off:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class,
            new_msg_bytes,
            (msg_type, new_payload_len)
        )
Example #16
0
    def convert_to_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:

        if not isinstance(msg, GatewayHelloMessageV2):
            raise TypeError("GatewayHelloMessage is expected")

        msg_bytes = msg.rawbytes()
        mem_view = memoryview(msg_bytes)

        result_bytes = bytearray(
            len(msg_bytes) - GatewayHelloMessageV2.ADDITIONAL_LENGTH)
        payload_len = GatewayHelloMessageV1.PAYLOAD_LENGTH

        result_bytes[:] = mem_view[:len(msg_bytes) -
                                   GatewayHelloMessageV2.ADDITIONAL_LENGTH]

        return AbstractBloxrouteMessage.initialize_class(
            GatewayHelloMessageV1, result_bytes,
            (GatewayMessageType.HELLO, payload_len))
Example #17
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:

        if not isinstance(msg, GatewayHelloMessageV1):
            raise TypeError("GatewayHelloMessageV1 is expected")

        msg_bytes = msg.rawbytes()
        mem_view = memoryview(msg_bytes)

        result_bytes = bytearray(
            len(msg_bytes) + constants.NODE_ID_SIZE_IN_BYTES)
        payload_len = GatewayHelloMessageV2.PAYLOAD_LENGTH
        off = 0
        result_bytes[off:len(msg_bytes)] = mem_view[:]
        off = len(msg_bytes)
        struct.pack_into("%ss" % constants.NODE_ID_SIZE_IN_BYTES, result_bytes,
                         off, b'')

        return AbstractBloxrouteMessage.initialize_class(
            GatewayHelloMessageV2, result_bytes,
            (GatewayMessageType.HELLO, payload_len))
Example #18
0
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected old message type from v10: {msg_type}"
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + self._LENGTH_DIFFERENCE

        new_msg_bytes = bytearray(self._NEW_MESSAGE_LEN)

        new_msg_bytes[:self._BREAKPOINT] = msg.rawbytes()[:self._BREAKPOINT]

        new_msg_bytes[self._BREAKPOINT +
                      self._LENGTH_DIFFERENCE:] = msg.rawbytes()[self.
                                                                 _BREAKPOINT:]

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
    def convert_to_older_version(
        self, msg: AbstractInternalMessage
    ) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Tried to convert unexpected new "
                f"message type to v7: {msg_type}"
            )

        old_version_msg_class = self._MSG_TYPE_TO_OLD_MSG_CLASS_MAPPING[
            msg_type
        ]
        old_version_payload_len = (
            msg.payload_len()
            - constants.QUOTA_FLAG_LEN
            - constants.UL_INT_SIZE_IN_BYTES
        )

        old_version_msg_bytes = bytearray(
            self._LEFT_BREAKPOINT
            + len(msg.rawbytes()[self._RIGHT_BREAKPOINT :])
        )
        old_version_msg_bytes[: self._LEFT_BREAKPOINT] = msg.rawbytes()[
            : self._LEFT_BREAKPOINT
        ]
        old_version_msg_bytes[self._LEFT_BREAKPOINT :] = msg.rawbytes()[
            self._RIGHT_BREAKPOINT :
        ]

        return AbstractBloxrouteMessage.initialize_class(
            old_version_msg_class,
            old_version_msg_bytes,
            (msg_type, old_version_payload_len),
        )
    def convert_from_older_version(
            self, msg: AbstractInternalMessage) -> AbstractInternalMessage:
        msg_type = msg.MESSAGE_TYPE

        if msg_type not in self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING:
            raise ValueError(
                f"Unexpected got message of type {msg_type}. Could not convert to current version."
            )

        new_msg_class = self._MSG_TYPE_TO_NEW_MSG_CLASS_MAPPING[msg_type]
        new_payload_len = msg.payload_len() + constants.CONTROL_FLAGS_LEN
        new_msg_bytes = bytearray(AbstractBloxrouteMessage.HEADER_LENGTH +
                                  new_payload_len)
        new_msg_bytes[:constants.
                      STARTING_SEQUENCE_BYTES_LEN] = constants.STARTING_SEQUENCE_BYTES
        struct.pack_into("<12sL", new_msg_bytes,
                         constants.STARTING_SEQUENCE_BYTES_LEN, msg_type,
                         new_payload_len)
        new_msg_bytes[constants.STARTING_SEQUENCE_BYTES_LEN:-constants.
                      CONTROL_FLAGS_LEN] = msg.rawbytes()
        new_msg_bytes[-1] = BloxrouteMessageControlFlags.VALID

        return AbstractBloxrouteMessage.initialize_class(
            new_msg_class, new_msg_bytes, (msg_type, new_payload_len))
Example #21
0
    def test_asbstract_bloxroute_message(self):
        total_msg_len = 1000
        msg_type = b"dummy_msg"

        payload_len = total_msg_len - constants.BX_HDR_COMMON_OFF - constants.STARTING_SEQUENCE_BYTES_LEN
        buffer = bytearray(total_msg_len)
        message = AbstractBloxrouteMessage(msg_type=msg_type, payload_len=payload_len, buf=buffer)

        raw_bytes = message.rawbytes()
        self.assertEqual(total_msg_len, len(raw_bytes))
        self.assertEqual(msg_type, message.msg_type())
        self.assertEqual(payload_len, message.payload_len())
        self.assertEqual(payload_len, len(message.payload()))

        self.assertTrue(message.get_control_flags() & BloxrouteMessageControlFlags.VALID)

        message.remove_control_flag(BloxrouteMessageControlFlags.VALID)
        self.assertFalse(message.get_control_flags() & BloxrouteMessageControlFlags.VALID)

        message.set_control_flag(BloxrouteMessageControlFlags.VALID)
        self.assertTrue(message.get_control_flags() & BloxrouteMessageControlFlags.VALID)

        # Trying set already set flag
        message.set_control_flag(BloxrouteMessageControlFlags.VALID)
        self.assertTrue(message.get_control_flags() & BloxrouteMessageControlFlags.VALID)
Example #22
0
class MessageTest(AbstractTestCase):
    def setUp(self):
        self.buf1 = bytearray([i for i in range(40)])
        self.payload_len1 = 20
        self.msg_type1 = b"example"
        self.message1 = AbstractBloxrouteMessage(msg_type=self.msg_type1,
                                                 payload_len=self.payload_len1,
                                                 buf=self.buf1)
        self.buf2 = bytearray([i for i in range(24)])
        self.payload_len2 = 50
        self.msg_type2 = b"hello"
        self.message2 = AbstractBloxrouteMessage(msg_type=self.msg_type2,
                                                 payload_len=self.payload_len2,
                                                 buf=self.buf2)

    def test_init(self):
        with self.assertRaises(struct.error):
            AbstractBloxrouteMessage(msg_type=None,
                                     payload_len=20,
                                     buf=bytearray([i for i in range(40)]))
        with self.assertRaises(ValueError):
            AbstractBloxrouteMessage(msg_type=b"hello",
                                     payload_len=-5,
                                     buf=bytearray([i for i in range(40)]))
        with self.assertRaises(ValueError):
            AbstractBloxrouteMessage(msg_type=b"hello",
                                     payload_len=20,
                                     buf=bytearray([i for i in range(10)]))

        self.assertEqual(self.buf1, self.message1.buf)
        self.assertEqual(self.buf1, self.message1._memoryview)
        self.assertEqual(self.msg_type1, self.message1._msg_type)
        self.assertIsNone(self.message1._payload)
        self.assertEqual(self.payload_len1, self.message1._payload_len)

    def test_rawbytes(self):
        self.assertIsInstance(self.message1.rawbytes(), memoryview)
        self.assertEqual(self.payload_len1, self.message1._payload_len)
        message2_rawbytes = self.message2.rawbytes()
        self.assertEqual(self.payload_len2, self.message2._payload_len)
        self.assertEqual(message2_rawbytes,
                         memoryview(self.message2._memoryview))

    def test_msg_type(self):
        self.assertEqual(self.msg_type1, self.message1.msg_type())

    def test_payload_len(self):
        self.assertEqual(self.payload_len1, self.message1.payload_len())

    def test_payload(self):
        self.assertIsNone(self.message1._payload)
        self.assertEqual(
            self.
            buf1[AbstractBloxrouteMessage.HEADER_LENGTH:self.payload_len1 +
                 AbstractBloxrouteMessage.HEADER_LENGTH],
            self.message1.payload())
        self.assertEqual(
            self.
            buf1[AbstractBloxrouteMessage.HEADER_LENGTH:self.payload_len1 +
                 AbstractBloxrouteMessage.HEADER_LENGTH],
            self.message1._payload)