Exemplo n.º 1
0
    def __init__(
        self,
        begin_string=None,
        body_length=None,
        message_type=None,
        message_seq_num=None,
        encoded_body=None,
        checksum=None,
    ):

        if begin_string is None:
            begin_string = settings.BEGIN_STRING

        if encoded_body is None:
            encoded_body = b""

        self.encoded_body = encoded_body

        if body_length is None:
            body_length = len(encoded_body)

        if checksum is None:
            checksum = utils.calculate_checksum(encoded_body)

        super().__init__(
            (connection.protocol.Tag.BeginString, begin_string),
            (connection.protocol.Tag.BodyLength, body_length),
            (connection.protocol.Tag.MsgType, message_type),
            (connection.protocol.Tag.MsgSeqNum, message_seq_num),
            (connection.protocol.Tag.CheckSum, checksum),
        )
Exemplo n.º 2
0
    def check_checksum(
        cls, data: bytes, body_start: int = 0, body_end: int = None
    ) -> Tuple[int, int]:
        """
        Checks the Checksum tag (10) for the encoded message data provided.

        :param data: An encoded FIX message.
        :param body_start: The index in the encoded message at which the message body starts.
        :param body_end: The index in the encoded message at which the message body ends.
        If this value is not provided, then it will default to the index at which the Checksum tag starts.

        :return: A tuple consisting of the value of the checksum and the index at which the tag ends.

        :raises: ParsingError if the BeginString tag can either not be found, or if it is not the first tag
        in the message.
        """
        try:
            checksum, checksum_start, checksum_end = utils.rindex_tag(10, data)
        except TagNotFound as e:
            raise ParsingError(
                f"Checksum (10) not found in {utils.decode(data)}."
            ) from e

        if len(data) != checksum_end + 1:
            raise ParsingError(
                f"Unexpected bytes following checksum: {data[checksum_start:]}."
            )

        if body_end is None:
            body_end = checksum_start

        checksum = int(checksum)
        calc_checksum = utils.calculate_checksum(data[body_start:body_end])
        if checksum != calc_checksum:
            raise ParsingError(
                f"Checksum check failed. Calculated value of {calc_checksum} does not match {checksum}."
            )

        return checksum, checksum_end
Exemplo n.º 3
0
def test_checksum():
    assert (utils.calculate_checksum(
        b"8=FIXT.1.1\x019=75\x0135=A\x0134=1\x0149=ROFX\x0152=20170417-18:29:09.599\x0156=eco\x0198=0\x01"
        + b"108=20\x01141=Y\x011137=9\x01") == 79)