@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.anyInt ;
import static org.mockito.ArgumentMatchers.anyInt ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.Mockito.doReturn ;
import static org.mockito.Mockito.lenient ;
import static org.mockito.Mockito.lenient ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.spy ;
import static org.mockito.Mockito.spy ;
@ -52,6 +53,7 @@ import org.hyperledger.besu.nat.NatService;
import org.hyperledger.besu.nat.core.domain.NetworkProtocol ;
import org.hyperledger.besu.nat.core.domain.NetworkProtocol ;
import org.hyperledger.besu.nat.upnp.UpnpNatManager ;
import org.hyperledger.besu.nat.upnp.UpnpNatManager ;
import java.net.InetAddress ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.List ;
import java.util.List ;
@ -61,6 +63,10 @@ import java.util.stream.Collectors;
import java.util.stream.Stream ;
import java.util.stream.Stream ;
import org.apache.tuweni.bytes.Bytes ;
import org.apache.tuweni.bytes.Bytes ;
import org.apache.tuweni.bytes.Bytes32 ;
import org.apache.tuweni.crypto.SECP256K1 ;
import org.apache.tuweni.devp2p.EthereumNodeRecord ;
import org.apache.tuweni.discovery.DNSDaemonListener ;
import org.assertj.core.api.Assertions ;
import org.assertj.core.api.Assertions ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
@ -72,8 +78,11 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith ( MockitoJUnitRunner . StrictStubs . class )
@RunWith ( MockitoJUnitRunner . StrictStubs . class )
public final class DefaultP2PNetworkTest {
public final class DefaultP2PNetworkTest {
final MaintainedPeers maintainedPeers = new MaintainedPeers ( ) ;
final MaintainedPeers maintainedPeers = new MaintainedPeers ( ) ;
final SECP256K1 . SecretKey mockKey =
SECP256K1 . SecretKey . fromBytes (
Bytes32 . fromHexString (
"0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63" ) ) ;
@Mock PeerDiscoveryAgent discoveryAgent ;
@Mock PeerDiscoveryAgent discoveryAgent ;
@Mock RlpxAgent rlpxAgent ;
@Mock RlpxAgent rlpxAgent ;
@ -334,18 +343,15 @@ public final class DefaultP2PNetworkTest {
}
}
@Test
@Test
public void shouldNotStartDnsDiscoveryWhenDNSURLIsNotConfigured ( ) {
public void shouldNotStartDnsDiscoveryWhenDnsURLIsNotConfigured ( ) {
// spy on DefaultP2PNetwork
DefaultP2PNetwork testClass = network ( ) ;
DefaultP2PNetwork testClass = spy ( network ( ) ) ;
testClass . start ( ) ;
testClass . start ( ) ;
// ensure we called getDnsDaemon during start, and that it is NOT present:
// ensure DnsDaemon is NOT present:
verify ( testClass , times ( 1 ) ) . getDnsDaemon ( ) ;
assertThat ( testClass . getDnsDaemon ( ) ) . isNotPresent ( ) ;
assertThat ( testClass . getDnsDaemon ( ) ) . isNotPresent ( ) ;
}
}
@Test
@Test
public void shouldStartDnsDiscoveryWhenDNSURLIsNot Configured ( ) {
public void shouldStartDnsDiscoveryWhenDnsURLIs Configured ( ) {
// create a discovery config with a dns config
// create a discovery config with a dns config
DiscoveryConfiguration disco =
DiscoveryConfiguration disco =
DiscoveryConfiguration . create ( ) . setDnsDiscoveryURL ( "enrtree://mock@localhost" ) ;
DiscoveryConfiguration . create ( ) . setDnsDiscoveryURL ( "enrtree://mock@localhost" ) ;
@ -355,12 +361,58 @@ public final class DefaultP2PNetworkTest {
when ( spy ( config ) . getDiscovery ( ) ) . thenReturn ( disco ) . getMock ( ) ;
when ( spy ( config ) . getDiscovery ( ) ) . thenReturn ( disco ) . getMock ( ) ;
// spy on DefaultP2PNetwork
// spy on DefaultP2PNetwork
DefaultP2PNetwork testClass = spy ( ( DefaultP2PNetwork ) builder ( ) . config ( dnsConfig ) . build ( ) ) ;
DefaultP2PNetwork testClass = ( DefaultP2PNetwork ) builder ( ) . config ( dnsConfig ) . build ( ) ;
testClass . start ( ) ;
assertThat ( testClass . getDnsDaemon ( ) ) . isPresent ( ) ;
}
@Test
public void shouldUseDnsServerOverrideIfPresent ( ) {
// create a discovery config with a dns config
DiscoveryConfiguration disco =
DiscoveryConfiguration . create ( ) . setDnsDiscoveryURL ( "enrtree://mock@localhost" ) ;
// spy on config to return dns discovery config:
NetworkingConfiguration dnsConfig = spy ( config ) ;
doReturn ( disco ) . when ( dnsConfig ) . getDiscovery ( ) ;
doReturn ( Optional . of ( "localhost" ) ) . when ( dnsConfig ) . getDnsDiscoveryServerOverride ( ) ;
DefaultP2PNetwork testClass = ( DefaultP2PNetwork ) builder ( ) . config ( dnsConfig ) . build ( ) ;
testClass . start ( ) ;
testClass . start ( ) ;
// ensure we called getDnsDaemon during start, and that it is present:
verify ( testClass , times ( 1 ) ) . getDnsDaemon ( ) ;
// ensure we used the dns server override config when building DNSDaemon:
assertThat ( testClass . getDnsDaemon ( ) ) . isPresent ( ) ;
assertThat ( testClass . getDnsDaemon ( ) ) . isPresent ( ) ;
verify ( dnsConfig , times ( 2 ) ) . getDnsDiscoveryServerOverride ( ) ;
}
@Test
public void shouldNotDropDnsHostsOnEmptyLookup ( ) {
DefaultP2PNetwork network = network ( ) ;
DNSDaemonListener listenerUnderTest = network . createDaemonListener ( ) ;
// assert no entries prior to lookup
assertThat ( network . dnsPeers . get ( ) ) . isNull ( ) ;
// simulate successful lookup of 1 peer
listenerUnderTest . newRecords (
1 ,
List . of (
EthereumNodeRecord . create (
SECP256K1 . KeyPair . fromSecretKey ( mockKey ) ,
1L ,
null ,
null ,
InetAddress . getLoopbackAddress ( ) ,
30303 ,
30303 ) ) ) ;
assertThat ( network . dnsPeers . get ( ) ) . isNotEmpty ( ) ;
assertThat ( network . dnsPeers . get ( ) . size ( ) ) . isEqualTo ( 1 ) ;
// simulate failed lookup empty list
listenerUnderTest . newRecords ( 2 , Collections . emptyList ( ) ) ;
assertThat ( network . dnsPeers . get ( ) ) . isNotEmpty ( ) ;
assertThat ( network . dnsPeers . get ( ) . size ( ) ) . isEqualTo ( 1 ) ;
}
}
private DefaultP2PNetwork network ( ) {
private DefaultP2PNetwork network ( ) {