use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.
the class PeerDiscoveryAgentTest method peerTable_disallowPeer.
@Test
public void peerTable_disallowPeer() {
// Setup peer
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().bootstrapPeers(remotePeer).peerPermissions(peerPermissions));
when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(false);
agent.start(999);
assertThat(agent.streamDiscoveredPeers()).hasSize(0);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.
the class PeerDiscoveryAgentTest method bond_supplyGenericPeer.
@Test
public void bond_supplyGenericPeer() {
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
final Peer genericPeer = DefaultPeer.fromEnodeURL(remotePeer.getEnodeURL());
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().peerPermissions(peerPermissions));
when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
// Start agent and bond
assertThat(agent.start(30303)).isCompleted();
assertThat(agent.streamDiscoveredPeers()).isEmpty();
agent.bond(genericPeer);
// We should send an outgoing ping
List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
assertThat(remoteIncomingPackets).hasSize(2);
final IncomingPacket firstMsg = remoteIncomingPackets.get(0);
assertThat(firstMsg.packet.getType()).isEqualTo(PacketType.PING);
// The remote peer will send a PING and we'll respond with a return PONG
assertThat(firstMsg.fromAgent).isEqualTo(agent);
final IncomingPacket secondMsg = remoteIncomingPackets.get(1);
assertThat(secondMsg.packet.getType()).isEqualTo(PacketType.PONG);
assertThat(secondMsg.fromAgent).isEqualTo(agent);
// The peer should now be bonded
assertThat(agent.streamDiscoveredPeers()).contains(remotePeer);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.
the class PeerDiscoveryAgentTest method peerTable_allowPeer.
@Test
public void peerTable_allowPeer() {
// Setup peer
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
final DiscoveryPeer remotePeer = otherNode.getAdvertisedPeer().get();
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
final MockPeerDiscoveryAgent agent = helper.createDiscoveryAgent(helper.agentBuilder().bootstrapPeers(remotePeer).peerPermissions(peerPermissions));
when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(false);
when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(true);
agent.start(999);
assertThat(agent.streamDiscoveredPeers()).hasSize(1);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.
the class PeerDiscoveryAgentTest method bonding_disallowIncomingBonding.
@Test
public void bonding_disallowIncomingBonding() {
// Start an agent with no bootstrap peers.
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList(), peerPermissions);
assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
final Peer localNode = agent.getAdvertisedPeer().get();
// Setup peer and permissions
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
final Peer remotePeer = otherNode.getAdvertisedPeer().get();
when(peerPermissions.isPermitted(eq(localNode), any(), any())).thenReturn(true);
when(peerPermissions.isPermitted(eq(localNode), eq(remotePeer), eq(Action.DISCOVERY_ACCEPT_INBOUND_BONDING))).thenReturn(false);
// Bond
bondViaIncomingPing(agent, otherNode);
// Check peer was not allowed to connect
assertThat(agent.streamDiscoveredPeers()).hasSize(0);
// Check that peer did not receive a return pong
assertThat(otherNode.getIncomingPackets()).isEmpty();
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent in project besu by hyperledger.
the class PeerDiscoveryAgentTest method bonding_allowIncomingBonding.
@Test
public void bonding_allowIncomingBonding() {
// Start an agent with no bootstrap peers.
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList(), peerPermissions);
assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
final DiscoveryPeer localNode = agent.getAdvertisedPeer().get();
// Setup peer and permissions
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
final Peer remotePeer = otherNode.getAdvertisedPeer().get();
when(peerPermissions.isPermitted(eq(localNode), any(), any())).thenReturn(false);
when(peerPermissions.isPermitted(eq(localNode), eq(remotePeer), eq(Action.DISCOVERY_ACCEPT_INBOUND_BONDING))).thenReturn(true);
when(peerPermissions.isPermitted(any(), eq(remotePeer), eq(Action.DISCOVERY_ALLOW_IN_PEER_TABLE))).thenReturn(true);
// Bond
otherNode.bond(localNode);
List<IncomingPacket> remoteIncomingPackets = otherNode.getIncomingPackets();
assertThat(remoteIncomingPackets).hasSize(2);
final IncomingPacket firstMsg = remoteIncomingPackets.get(0);
assertThat(firstMsg.packet.getType()).isEqualTo(PacketType.PING);
assertThat(firstMsg.fromAgent).isEqualTo(agent);
// Check that peer received a return pong
final IncomingPacket secondMsg = remoteIncomingPackets.get(1);
assertThat(secondMsg.packet.getType()).isEqualTo(PacketType.PONG);
assertThat(secondMsg.fromAgent).isEqualTo(agent);
}
Aggregations