Esempio n. 1
0
def test_bolt_01_csv():
    # We can create a namespace from the csv.
    ns = MessageNamespace(bolt1.csv)

    # string [expected string]
    for t in [['init globalfeatures= features=80',
               'init globalfeatures= features=80 tlvs={}'],
              ['init globalfeatures= features=80 tlvs={}'],
              ['init globalfeatures= features=80 tlvs={networks={chains=[6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000]}}'],
              ['init globalfeatures= features=80 tlvs={networks={chains=[6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000,1fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000]}}'],
              ['error channel_id=0000000000000000000000000000000000000000000000000000000000000000 data=00'],
              ['ping num_pong_bytes=0 ignored='],
              ['ping num_pong_bytes=3 ignored=0000'],
              ['pong ignored='],
              ['pong ignored=000000']]:
        m = Message.from_str(bolt1.namespace, t[0])
        b = io.BytesIO()
        m.write(b)

        # Works with our manually-made namespace, and the builtin one.
        b.seek(0)
        m2 = Message.read(bolt1.namespace, b)
        assert m2.to_str() == t[-1]

        b.seek(0)
        m2 = Message.read(ns, b)
        assert m2.to_str() == t[-1]
Esempio n. 2
0
def test_tlv_complex():
    # A real example from the spec.
    ns = MessageNamespace([
        "msgtype,reply_channel_range,264,gossip_queries",
        "msgdata,reply_channel_range,chain_hash,chain_hash,",
        "msgdata,reply_channel_range,first_blocknum,u32,",
        "msgdata,reply_channel_range,number_of_blocks,u32,",
        "msgdata,reply_channel_range,full_information,byte,",
        "msgdata,reply_channel_range,len,u16,",
        "msgdata,reply_channel_range,encoded_short_ids,byte,len",
        "msgdata,reply_channel_range,tlvs,reply_channel_range_tlvs,",
        "tlvtype,reply_channel_range_tlvs,timestamps_tlv,1",
        "tlvdata,reply_channel_range_tlvs,timestamps_tlv,encoding_type,byte,",
        "tlvdata,reply_channel_range_tlvs,timestamps_tlv,encoded_timestamps,byte,...",
        "tlvtype,reply_channel_range_tlvs,checksums_tlv,3",
        "tlvdata,reply_channel_range_tlvs,checksums_tlv,checksums,channel_update_checksums,...",
        "subtype,channel_update_timestamps",
        "subtypedata,channel_update_timestamps,timestamp_node_id_1,u32,",
        "subtypedata,channel_update_timestamps,timestamp_node_id_2,u32,",
        "subtype,channel_update_checksums",
        "subtypedata,channel_update_checksums,checksum_node_id_1,u32,",
        "subtypedata,channel_update_checksums,checksum_node_id_2,u32,"
    ])

    binmsg = bytes.fromhex(
        '010806226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f000000670000000701001100000067000001000000006d000001000003101112fa300000000022d7a4a79bece840'
    )
    msg = Message.read(ns, io.BytesIO(binmsg))
    buf = io.BytesIO()
    msg.write(buf)
    assert buf.getvalue() == binmsg
Esempio n. 3
0
def test_subtype():
    ns = MessageNamespace()
    ns.load_csv([
        'msgtype,test1,1',
        'msgdata,test1,test_sub,channel_update_timestamps,4',
        'subtype,channel_update_timestamps',
        'subtypedata,' + 'channel_update_timestamps,timestamp_node_id_1,u32,',
        'subtypedata,' + 'channel_update_timestamps,timestamp_node_id_2,u32,'
    ])

    for test in [[
            "test1 test_sub=["
            "{timestamp_node_id_1=1,timestamp_node_id_2=2}"
            ",{timestamp_node_id_1=3,timestamp_node_id_2=4}"
            ",{timestamp_node_id_1=5,timestamp_node_id_2=6}"
            ",{timestamp_node_id_1=7,timestamp_node_id_2=8}]",
            bytes([0, 1] + [0, 0, 0, 1, 0, 0, 0, 2] +
                  [0, 0, 0, 3, 0, 0, 0, 4] + [0, 0, 0, 5, 0, 0, 0, 6] +
                  [0, 0, 0, 7, 0, 0, 0, 8])
    ]]:
        m = Message.from_str(ns, test[0])
        assert m.to_str() == test[0]
        buf = io.BytesIO()
        m.write(buf)
        assert buf.getvalue() == test[1]
        assert Message.read(ns, io.BytesIO(test[1])).to_str() == test[0]

    # Test missing field logic.
    m = Message.from_str(ns, "test1", incomplete_ok=True)
    assert m.missing_fields()
Esempio n. 4
0
    def action(self, runner: 'Runner') -> bool:
        super().action(runner)
        conn = self.find_conn(runner)
        while True:
            binmsg = runner.get_output_message(conn, self)
            if binmsg is None:
                raise EventError(self, "Did not receive a message from runner")

            for e in conn.must_not_events:
                if e.matches(binmsg):
                    raise EventError(self, "Got msg banned by {}: {}"
                                     .format(e, binmsg.hex()))

            # Might be completely unknown to namespace.
            try:
                msg = Message.read(event_namespace, io.BytesIO(binmsg))
            except ValueError as ve:
                raise EventError(self, "Runner gave bad msg {}: {}".format(binmsg.hex(), ve))

            # Ignore function may tell us to respond.
            response = self.ignore(msg)
            if response is not None:
                for msg in response:
                    binm = io.BytesIO()
                    msg.write(binm)
                    runner.recv(self, conn, binm.getvalue())
                continue

            err = self.message_match(runner, msg)
            if err:
                raise EventError(self, "{}: message was {}".format(err, msg.to_str()))

            break
        return True
def test_subtype_array():
    ns = MessageNamespace()
    ns.load_csv([
        'msgtype,tx_signatures,1', 'msgdata,tx_signatures,num_witnesses,u16,',
        'msgdata,tx_signatures,witness_stack,witness_stack,num_witnesses',
        'subtype,witness_stack',
        'subtypedata,witness_stack,num_input_witness,u16,',
        'subtypedata,witness_stack,witness_element,witness_element,num_input_witness',
        'subtype,witness_element', 'subtypedata,witness_element,len,u16,',
        'subtypedata,witness_element,witness,byte,len'
    ])

    for test in [[
            "tx_signatures witness_stack="
            "[{witness_element=[{witness=3045022100ac0fdee3e157f50be3214288cb7f11b03ce33e13b39dadccfcdb1a174fd3729a02200b69b286ac9f0fc5c51f9f04ae5a9827ac11d384cc203a0eaddff37e8d15c1ac01},{witness=02d6a3c2d0cf7904ab6af54d7c959435a452b24a63194e1c4e7c337d3ebbb3017b}]}]",
            bytes.fromhex(
                '00010001000200483045022100ac0fdee3e157f50be3214288cb7f11b03ce33e13b39dadccfcdb1a174fd3729a02200b69b286ac9f0fc5c51f9f04ae5a9827ac11d384cc203a0eaddff37e8d15c1ac01002102d6a3c2d0cf7904ab6af54d7c959435a452b24a63194e1c4e7c337d3ebbb3017b'
            )
    ]]:
        m = Message.from_str(ns, test[0])
        assert m.to_str() == test[0]
        buf = io.BytesIO()
        m.write(buf)
        assert buf.getvalue().hex() == test[1].hex()
        assert Message.read(ns, io.BytesIO(test[1])).to_str() == test[0]
Esempio n. 6
0
def test_tlv():
    ns = MessageNamespace()
    ns.load_csv([
        'msgtype,test1,1', 'msgdata,test1,tlvs,test_tlvstream,',
        'tlvtype,test_tlvstream,tlv1,1',
        'tlvdata,test_tlvstream,tlv1,field1,byte,4',
        'tlvdata,test_tlvstream,tlv1,field2,u32,',
        'tlvtype,test_tlvstream,tlv2,255',
        'tlvdata,test_tlvstream,tlv2,field3,byte,...'
    ])

    for test in [
        [
            "test1 tlvs={tlv1={field1=01020304,field2=5}}",
            bytes([0, 1] + [1, 8, 1, 2, 3, 4, 0, 0, 0, 5])
        ],
        [
            "test1 tlvs={tlv1={field1=01020304,field2=5},tlv2={field3=01020304}}",
            bytes([0, 1] + [1, 8, 1, 2, 3, 4, 0, 0, 0, 5] +
                  [253, 0, 255, 4, 1, 2, 3, 4])
        ],
        [
            "test1 tlvs={tlv1={field1=01020304,field2=5},4=010203,tlv2={field3=01020304}}",
            bytes([0, 1] + [1, 8, 1, 2, 3, 4, 0, 0, 0, 5] + [4, 3, 1, 2, 3] +
                  [253, 0, 255, 4, 1, 2, 3, 4])
        ]
    ]:
        m = Message.from_str(ns, test[0])
        assert m.to_str() == test[0]
        buf = io.BytesIO()
        m.write(buf)
        assert buf.getvalue() == test[1]
        assert Message.read(ns, io.BytesIO(test[1])).to_str() == test[0]

    # Ordering test (turns into canonical ordering)
    m = Message.from_str(
        ns,
        'test1 tlvs={tlv1={field1=01020304,field2=5},tlv2={field3=01020304},4=010203}'
    )
    buf = io.BytesIO()
    m.write(buf)
    assert buf.getvalue() == bytes([0, 1] + [1, 8, 1, 2, 3, 4, 0, 0, 0, 5] +
                                   [4, 3, 1, 2, 3] +
                                   [253, 0, 255, 4, 1, 2, 3, 4])
Esempio n. 7
0
def test_static_array():
    ns = MessageNamespace()
    ns.load_csv(['msgtype,test1,1', 'msgdata,test1,test_arr,byte,4'])
    ns.load_csv(
        ['msgtype,test2,2', 'msgdata,test2,test_arr,short_channel_id,4'])

    for test in [["test1 test_arr=00010203",
                  bytes([0, 1] + [0, 1, 2, 3])],
                 [
                     "test2 test_arr=[0x1x2,4x5x6,7x8x9,10x11x12]",
                     bytes([0, 2] + [0, 0, 0, 0, 0, 1, 0, 2] +
                           [0, 0, 4, 0, 0, 5, 0, 6] +
                           [0, 0, 7, 0, 0, 8, 0, 9] +
                           [0, 0, 10, 0, 0, 11, 0, 12])
                 ]]:
        m = Message.from_str(ns, test[0])
        assert m.to_str() == test[0]
        buf = io.BytesIO()
        m.write(buf)
        assert buf.getvalue() == test[1]
        assert Message.read(ns, io.BytesIO(test[1])).to_str() == test[0]
Esempio n. 8
0
    def action(self, runner: 'Runner') -> bool:
        super().action(runner)

        # Check they all use the same conn!
        conn: Optional[Conn] = None
        for s in self.sequences:
            c = cast(ExpectMsg, s.events[0]).find_conn(runner)
            if conn is None:
                conn = c
            elif c != conn:
                raise SpecFileError(self, "sequences do not all use the same conn?")
        assert conn

        while True:
            binmsg = runner.get_output_message(conn, self.sequences[0].events[0])
            if binmsg is None:
                raise EventError(self, "Did not receive a message from runner")

            try:
                msg = Message.read(namespace(), io.BytesIO(binmsg))
            except ValueError as ve:
                raise EventError(self, "Invalid msg {}: {}".format(binmsg.hex(), ve))

            ignored = Sequence.ignored_by_all(msg,
                                              self.enabled_sequences(runner))
            # If they gave us responses, send those now.
            if ignored is not None:
                for msg in ignored:
                    binm = io.BytesIO()
                    msg.write(binm)
                    runner.recv(self, conn, binm.getvalue())
                continue

            seq = Sequence.match_which_sequence(runner, msg, self.enabled_sequences(runner))
            if seq is not None:
                # We found the sequence, run it
                return seq.action(runner, skip_first=True)

            raise EventError(self,
                             "None of the sequences {} matched {}".format(self.enabled_sequences(runner), msg.to_str()))
Esempio n. 9
0
    def action(self, runner: 'Runner') -> bool:
        super().action(runner)

        # Check they all use the same conn!
        conn = None
        for s in self.sequences:
            c = cast(ExpectMsg, s.events[0]).find_conn(runner)
            if conn is None:
                conn = c
            elif c != conn:
                raise SpecFileError(self, "sequences do not all use the same conn?")
        assert conn

        all_done = True
        sequences = self.enabled_sequences(runner)
        while sequences != []:
            # Get message
            binmsg = runner.get_output_message(conn, sequences[0].events[0])
            if binmsg is None:
                raise EventError(self, "Did not receive a message from runner, still expecting {}"
                                 .format([s.events[0] for s in sequences]))

            try:
                msg = Message.read(namespace(), io.BytesIO(binmsg))
            except ValueError as ve:
                raise EventError(self, "Invalid msg {}: {}".format(binmsg.hex(), ve))

            if Sequence.ignored_by_all(msg, sequences):
                continue

            seq = Sequence.match_which_sequence(runner, msg, sequences)
            if seq is not None:
                sequences.remove(seq)
                all_done &= seq.action(runner, skip_first=True)
                continue

            raise EventError(self,
                             "Message did not match any sequences {}: {}"
                             .format([s.events[0] for s in sequences], msg.to_str()))
        return all_done