def test_react_to_hello(self): conn = MagicMock() conf = MagicMock() conf.opt_peer_num = 10 node = Node(node_name='node', key='ffffffff') keys_auth = KeysAuth(self.path) keys_auth.key = node.key keys_auth.key_id = node.key peer_session = PeerSession(conn) peer_session.p2p_service = P2PService(node, conf, keys_auth, False) peer_session.p2p_service.metadata_manager = MagicMock() peer_session.send = MagicMock() peer_session.disconnect = MagicMock() peer_session._solve_challenge = MagicMock() def create_verify(value): def verify(*args): return value return verify key_id = 'deadbeef' peer_info = MagicMock() peer_info.key = key_id msg = MessageHello(port=1, node_name='node2', client_key_id=key_id, node_info=peer_info, proto_id=-1) peer_session.verify = create_verify(False) peer_session._react_to_hello(msg) peer_session.disconnect.assert_called_with(PeerSession.DCRUnverified) peer_session.verify = create_verify(True) peer_session._react_to_hello(msg) peer_session.disconnect.assert_called_with( PeerSession.DCRProtocolVersion) msg.proto_id = P2P_PROTOCOL_ID peer_session._react_to_hello(msg) assert key_id in peer_session.p2p_service.peers assert peer_session.p2p_service.peers[key_id] peer_session.p2p_service.peers[key_id] = MagicMock() conn.opened = True peer_session.key_id = None peer_session._react_to_hello(msg) peer_session.disconnect.assert_called_with( PeerSession.DCRDuplicatePeers)
def test_react_to_stop_gossip(self): conn = MagicMock() conf = MagicMock() conf.opt_peer_num = 10 node = Node(node_name='node', key='ffffffff') keys_auth = KeysAuth(self.path) keys_auth.key = node.key keys_auth.key_id = node.key peer_session = PeerSession(conn) peer_session.p2p_service = P2PService(node, conf, keys_auth, False) peer_session.key_id = "NEW KEY_ID" peer_session._react_to_stop_gossip(MessageStopGossip())
def test_react_to_hello(self): conn = MagicMock() node = Node(node_name='node', key='ffffffff') keys_auth = KeysAuth(self.path) keys_auth.key = node.key keys_auth.key_id = node.key ts = TaskSession(conn) ts.task_server = Mock() ts.disconnect = Mock() ts.send = Mock() def create_verify(value): def verify(*args): return value return verify key_id = 'deadbeef' peer_info = MagicMock() peer_info.key = key_id msg = MessageHello(port=1, node_name='node2', client_key_id=key_id, node_info=peer_info, proto_id=-1) ts.verify = create_verify(False) ts._react_to_hello(msg) ts.disconnect.assert_called_with(TaskSession.DCRUnverified) ts.verify = create_verify(True) ts._react_to_hello(msg) ts.disconnect.assert_called_with(TaskSession.DCRProtocolVersion) msg.proto_id = TASK_PROTOCOL_ID ts._react_to_hello(msg) assert ts.send.called