def test_protocol(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) # ping pong proto.send_ping() ping_packet = peer.packets.pop() proto._receive_ping(ping_packet) pong_packet = peer.packets.pop() proto._receive_pong(pong_packet) assert not peer.packets # hello (fails same nodeid) proto.send_hello() hello_packet = peer.packets.pop() proto._receive_hello(hello_packet) disconnect_packet = peer.packets.pop() # same nodeid assert disconnect_packet.cmd_id == P2PProtocol.disconnect.cmd_id assert not peer.stopped # FIXME: @heikoheiko this fails currently # hello (works) proto.send_hello() hello_packet = peer.packets.pop() peer.config['node']['id'] = '\x01' * 64 # change nodeid proto._receive_hello(hello_packet) assert not peer.packets assert not peer.stopped # FIXME: @heikoheiko this fails currently assert peer.hello_received # disconnect proto.send_disconnect(reason=proto.disconnect.reason.disconnect_requested) disconnect_packet = peer.packets.pop() proto._receive_disconnect(disconnect_packet) assert not peer.packets assert peer.stopped
def test_eip8_hello(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) test_packet = Packet(cmd_id=1, payload=eip8_hello) proto._receive_hello(test_packet) assert peer.hello_received assert peer.remote_client_version == "kneth/v0.91/plan9" assert peer.remote_hello_version == 22 assert peer.remote_pubkey == 'fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877'.decode( 'hex')
def test_callback(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) # setup callback r = [] def cb(_proto, **data): assert _proto == proto r.append(data) proto.receive_pong_callbacks.append(cb) # trigger proto.send_ping() ping_packet = peer.packets.pop() proto._receive_ping(ping_packet) pong_packet = peer.packets.pop() proto._receive_pong(pong_packet) assert not peer.packets assert len(r) == 1 assert r[0] == dict()
def test_session(): proto = P2PProtocol(peer=PeerMock(), service=WiredService(BaseApp())) hello_packet = proto.create_hello() responder_privkey = mk_privkey('secret1') responder = MultiplexedSession(responder_privkey, hello_packet=hello_packet) p0 = 0 responder.add_protocol(p0) initiator_privkey = mk_privkey('secret2') initiator = MultiplexedSession(initiator_privkey, hello_packet=hello_packet, remote_pubkey=privtopub(responder_privkey)) initiator.add_protocol(p0) # send auth msg = initiator.message_queue.get_nowait() assert msg # auth_init assert initiator.packet_queue.empty() assert not responder.is_initiator # receive auth responder.add_message(msg) assert responder.packet_queue.empty() assert responder.is_ready # send auth ack and hello ack_msg = responder.message_queue.get_nowait() hello_msg = responder.message_queue.get_nowait() assert hello_msg # receive auth ack & hello initiator.add_message(ack_msg + hello_msg) assert initiator.is_ready hello_packet = initiator.packet_queue.get_nowait() assert isinstance(hello_packet, Packet) # initiator sends hello hello_msg = initiator.message_queue.get_nowait() assert hello_msg # hello received by responder responder.add_message(hello_msg) hello_packet = responder.packet_queue.get_nowait() assert isinstance(hello_packet, Packet) # assert we received an actual hello packet data = proto.hello.decode_payload(hello_packet.payload) assert data['version'] # test normal operation ping = proto.create_ping() initiator.add_packet(ping) msg = initiator.message_queue.get_nowait() # receive ping responder.add_message(msg) ping_packet = responder.packet_queue.get_nowait() assert isinstance(ping_packet, Packet) data = proto.ping.decode_payload(ping_packet.payload) # reply with pong pong = proto.create_ping() responder.add_packet(pong) msg = responder.message_queue.get_nowait() # receive pong initiator.add_message(msg) pong_packet = initiator.packet_queue.get_nowait() assert isinstance(pong_packet, Packet) data = proto.pong.decode_payload(pong_packet.payload)