def test_load_peer_addresses_peer_xrd(self): """ Should load peers from peers.xrd AND from config.user.peer_list """ with set_xrd_dir('peers') as tmp_dir: self.peer_manager.peers_path = os.path.join(tmp_dir, 'peers.json') self.peer_manager.load_peer_addresses() # This was in peers.xrd self.assertIn(IPMetadata.canonical_full_address('34.208.138.15'), self.peer_manager.known_peer_addresses) # config.user.peer_list is all in there too for p in config.user.peer_list: self.assertIn(IPMetadata.canonical_full_address(p), self.peer_manager.known_peer_addresses)
def test_handle_peer_list_works(self, logger): """ Heavy error testing should be done in combine_peer_lists() and extend_known_peers(), which this fx uses. """ peer_list_message = xrdlegacy_pb2.LegacyMessage(func_name=xrdlegacy_pb2.LegacyMessage.PL, plData=xrdlegacy_pb2.PLData( peer_ips={'127.0.0.3:5000', '127.0.0.4:5001'}, public_port=9000)) channel = make_channel() channel.host = IPMetadata('187.0.0.1', 9000) channel.peer = IPMetadata('187.0.0.2', 9000) channel.ip_public_port = '187.0.0.1:9000' # handle_peer_list() will call extend_known_peers(), so we gotta mock it out. It's tested elsewhere anyway. self.peer_manager.handle_peer_list(channel, peer_list_message)
def monitor_connections(self): reactor.callLater(config.user.monitor_connections_interval, self.monitor_connections) if len(self._peer_connections) == 0: logger.warning('No Connected Peer Found') known_peers = self._xrd_node.peer_manager.load_known_peers() self._peer_q.extend(known_peers) connected_peers_set = set() for conn_protocol in self._peer_connections: connected_peers_set.add(conn_protocol.peer.full_address) for peer_item in config.user.peer_list: peer_metadata = IPMetadata.from_full_address(peer_item) if peer_metadata.full_address in self._peer_q: self._peer_q.remove(peer_metadata.full_address) if peer_metadata.full_address not in connected_peers_set: self.connect_peer([peer_metadata.full_address]) if len(self._peer_connections) >= config.user.max_peers_limit: return if len(self._peer_q) == 0: return peer_address_list = [] max_length = min(10, config.user.max_peers_limit) while len(self._peer_q) > 0 and len(peer_address_list) != max_length: peer_address_list.append(self._peer_q.pop(0)) self.connect_peer(peer_address_list)
def test_add_connection_wont_connect_to_itself(self, m_reactor, m_logger): """ P2PFactory should refuse to connect to a P2PProtocol which seems to have our own IP address P2PProtocol.host_ip should always be our IP address. If add_connection() detects this, then it will rebuild the peer_list, excluding this IP address. """ channel_4 = Mock(autospec=P2PProtocol, name='mock Channel 4', host=IPMetadata('4.4.4.4', config.user.p2p_public_port), peer=IPMetadata('4.4.4.4', config.user.p2p_public_port)) self.factory._xrd_node.peer_manager.known_peer_addresses = [make_address('1.1.1.1'), make_address('2.2.2.2'), make_address('3.3.3.3'), make_address('4.4.4.4')] self.assertFalse(self.factory.add_connection(channel_4)) self.assertEqual(self.factory.num_connections, 3) self.factory._xrd_node.peer_manager.extend_known_peers.assert_called_once_with( [make_address('1.1.1.1'), make_address('2.2.2.2'), make_address('3.3.3.3')])
def test_trusted_time(self, send, get_peer): channel = P2PProtocol() with mock.patch('xrd.core.misc.ntp.getTime') as time_mock: time_mock.return_value = channel.connected_at + 1 get_peer.return_value = IPMetadata('192.168.0.1', 1000) for _ in range(config.dev.trust_min_msgcount): buffer = bytes(hstr2bin('000000191a170a0776657273696f6e120c67656e657369735f68617368')) channel.dataReceived(buffer) time_mock.return_value = channel.connected_at + config.dev.trust_min_conntime + 1 self.assertTrue(self.peer_manager.trusted_peer(channel))
def test_load_peer_addresses_no_file(self): """ If no peers.xrd exists, use config.user.peer_list """ with set_xrd_dir('no_data') as tmp_dir: self.peer_manager.peers_path = os.path.join(tmp_dir, config.dev.peers_filename) self.peer_manager.load_peer_addresses() # config.user.peer_list is all in there too self.assertEqual(len(config.user.peer_list), len(self.peer_manager.known_peer_addresses)) for p in config.user.peer_list: self.assertIn(IPMetadata.canonical_full_address(p), self.peer_manager.known_peer_addresses)
def setUp(self): self.m_xrdnode = Mock(autospec=xrdNode, name='Fake xrdNode') self.m_xrdnode.peer_manager = Mock(autospec=P2PPeerManager, name='Fake PeerManager') self.m_xrdnode.peer_manager.is_banned.return_value = False self.m_xrdnode.peer_manager.load_known_peers.return_value = ['8.8.8.8', '9.9.9.9'] self.channel_1 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer=IPMetadata('1.1.1.1', config.user.p2p_public_port)) self.channel_2 = Mock(autospec=P2PProtocol, name='mock Channel 2', peer=IPMetadata('2.2.2.2', config.user.p2p_public_port)) self.channel_3 = Mock(autospec=P2PProtocol, name='mock Channel 3', peer=IPMetadata('3.3.3.3', config.user.p2p_public_port)) self.factory = P2PFactory(chain_manager=ChainManager(state=Mock(autospec=State)), sync_state=None, xrd_node=self.m_xrdnode) self.factory.pow = Mock(autospec=POW) self.factory.add_connection(self.channel_1) self.factory.add_connection(self.channel_2) self.factory.add_connection(self.channel_3)
def connect_peer(self, full_address_list): for full_address in full_address_list: try: addr = IPMetadata.from_full_address(full_address) connected_peers = self.get_connected_peer_addrs() should_connect = addr.full_address not in connected_peers if should_connect: reactor.connectTCP(addr.ip, addr.port, self) except Exception as e: logger.warning("Could not connect to %s - %s", full_address, str(e))
def test_add_connection_connection_limit(self, m_config, m_reactor, m_logger): """ When we've reached the connection_limit, add_connection() should refuse to do anything. In fact, it should disconnect the P2PProtocol. """ channel_4 = Mock(autospec=P2PProtocol, name='mock Channel 4', peer=IPMetadata('4.4.4.4', 9000)) m_config.user.max_peers_limit = 3 m_config.user.max_redundant_connections = 5 self.assertFalse(self.factory.add_connection(channel_4)) self.assertEqual(self.factory.num_connections, 3)
def test_add_connection_redundant_connection_limit(self, m_config, m_reactor, m_logger): """ When we've reached the max_redundant_connections, then the peer should be disconnected. """ channel_4 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer=IPMetadata('1.1.1.1', 9000)) m_config.user.max_peers_limit = 5 m_config.user.max_redundant_connections = 2 self.assertTrue(self.factory.add_connection(channel_4)) self.assertFalse(self.factory.add_connection(channel_4)) self.assertEqual(self.factory.num_connections, 4)
def test_basic_2(self): addr = IPMetadata.from_full_address('192.168.0.1:1234') self.assertEquals('192.168.0.1', addr.ip) self.assertEquals(1234, addr.port)
def test_global_1(self): with self.assertRaisesRegexp(ValueError, 'Local Peer IP Found 192.168.0.1:9000'): IPMetadata.from_full_address('192.168.0.1:9000', check_global=True)
def test_global_2(self): addr = IPMetadata.from_full_address('123.123.123.1:9000', check_global=True) self.assertEquals('123.123.123.1', addr.ip) self.assertEquals(9000, addr.port)
def test_wrong_port_3(self): with self.assertRaisesRegexp(ValueError, 'Invalid Peer Port 192.168.0.1:-1'): IPMetadata.from_full_address('192.168.0.1:-1')
def test_invalid_2(self): with self.assertRaisesRegexp(AddressValueError, 'Expected 4 octets in \'abc\''): IPMetadata.from_full_address('abc')
def test_invalid_1(self): with self.assertRaisesRegexp(AddressValueError, 'Address cannot be empty'): IPMetadata.from_full_address('')
def peer(self): return IPMetadata(self.transport.getPeer().host, self.transport.getPeer().port)
def host(self): return IPMetadata(self.transport.getHost().host, self.transport.getHost().port)
def test_basic_3(self): addr = IPMetadata.from_full_address('192.168.0.1') self.assertEquals('192.168.0.1', addr.ip) self.assertEquals(config.user.p2p_local_port, addr.port)
def make_channel(name=''): channel = Mock(autospec=P2PProtocol, name=name) channel.factory = Mock(autospec=P2PFactory) channel.peer = IPMetadata(ip_str="127.0.0.1", port=19009) return channel