@ -28,7 +28,8 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when ;
import org.hyperledger.besu.ethereum.p2p.peers.EnodeURL ;
import org.hyperledger.besu.ethereum.permissioning.node.NodeWhitelistUpdatedEvent ;
import org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController.NodesAllowlistResult ;
import org.hyperledger.besu.ethereum.permissioning.node.NodeAllowlistUpdatedEvent ;
import org.hyperledger.besu.metrics.BesuMetricCategory ;
import org.hyperledger.besu.plugin.services.MetricsSystem ;
import org.hyperledger.besu.plugin.services.metrics.Counter ;
@ -103,103 +104,84 @@ public class NodeLocalConfigPermissioningControllerTest {
@Test
public void whenAddNodesWithValidInputShouldReturnSuccess ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . SUCCESS ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . addNodes ( Lists . newArrayList ( enode1 ) ) ;
NodesAllowlistResult expected = new NodesAllowlistResult ( AllowlistOperationResult . SUCCESS ) ;
NodesAllowlistResult actualResult = controller . addNodes ( Lists . newArrayList ( enode1 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
assertThat ( controller . getNodesWhite list ( ) ) . containsExactly ( enode1 ) ;
assertThat ( controller . getNodesAllowlist ( ) ) . containsExactly ( enode1 ) ;
}
@Test
public void whenAddNodesInputHasExistingNodeShouldReturnAddErrorExistingEntry ( ) {
controller . addNodes ( Arrays . asList ( enode1 ) ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_EXISTING_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . addNodes ( Lists . newArrayList ( enode1 , enode2 ) ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_EXISTING_ENTRY ) ;
NodesAllowlistResult actualResult = controller . addNodes ( Lists . newArrayList ( enode1 , enode2 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenAddNodesInputHasDuplicatedNodesShouldReturnDuplicatedEntryError ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_DUPLICATED_ENTRY ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_DUPLICATED_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . addNodes ( Arrays . asList ( enode1 , enode1 ) ) ;
NodesAllowlistResult actualResult = controller . addNodes ( Arrays . asList ( enode1 , enode1 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenAddNodesInputHasEmptyListOfNodesShouldReturnErrorEmptyEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( new ArrayList < > ( ) ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( new ArrayList < > ( ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenAddNodesInputHasNullListOfNodesShouldReturnErrorEmptyEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( null ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( null ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenRemoveNodesInputHasAbsentNodeShouldReturnRemoveErrorAbsentEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_ABSENT_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( Lists . newArrayList ( enode1 , enode2 ) ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_ABSENT_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( Lists . newArrayList ( enode1 , enode2 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenRemoveNodesInputHasDuplicateNodesShouldReturnErrorDuplicatedEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_DUPLICATED_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( Lists . newArrayList ( enode1 , enode1 ) ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_DUPLICATED_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( Lists . newArrayList ( enode1 , enode1 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenRemoveNodesInputHasEmptyListOfNodesShouldReturnErrorEmptyEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( new ArrayList < > ( ) ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( new ArrayList < > ( ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@Test
public void whenRemoveNodesInputHasNullListOfNodesShouldReturnErrorEmptyEntry ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( null ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_EMPTY_ENTRY ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( null ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
}
@ -287,7 +269,7 @@ public class NodeLocalConfigPermissioningControllerTest {
@Test
public void
whenCheckingIfNodeIsPermittedDiscoveryPortShouldNotBeConsidered_whitelist edNodeHasDiscDisabled ( ) {
whenCheckingIfNodeIsPermittedDiscoveryPortShouldNotBeConsidered_allow edNodeHasDiscDisabled ( ) {
String peerWithDiscoveryPortSet =
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303?discport=0" ;
String peerWithoutDiscoveryPortSet =
@ -330,16 +312,16 @@ public class NodeLocalConfigPermissioningControllerTest {
List < String > newNode1 = singletonList ( EnodeURL . fromString ( enode1 ) . toString ( ) ) ;
List < String > newNode2 = singletonList ( EnodeURL . fromString ( enode2 ) . toString ( ) ) ;
assertThat ( controller . getNodesWhite list ( ) . size ( ) ) . isEqualTo ( 0 ) ;
assertThat ( controller . getNodesAllow list ( ) . size ( ) ) . isEqualTo ( 0 ) ;
controller . addNodes ( newNode1 ) ;
assertThat ( controller . getNodesWhite list ( ) . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( controller . getNodesAllow list ( ) . size ( ) ) . isEqualTo ( 1 ) ;
doThrow ( new IOException ( ) ) . when ( allowlistPersistor ) . updateConfig ( any ( ) , any ( ) ) ;
controller . addNodes ( newNode2 ) ;
assertThat ( controller . getNodesWhite list ( ) . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( controller . getNodesWhite list ( ) ) . isEqualTo ( newNode1 ) ;
assertThat ( controller . getNodesAllow list ( ) . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( controller . getNodesAllow list ( ) ) . isEqualTo ( newNode1 ) ;
verify ( allowlistPersistor , times ( 3 ) ) . verifyConfigFileMatchesState ( any ( ) , any ( ) ) ;
verify ( allowlistPersistor , times ( 2 ) ) . updateConfig ( any ( ) , any ( ) ) ;
@ -365,7 +347,7 @@ public class NodeLocalConfigPermissioningControllerTest {
controller . reload ( ) ;
assertThat ( controller . getNodesWhite list ( ) ) . containsExactly ( expectedEnodeURL ) ;
assertThat ( controller . getNodesAllow list ( ) ) . containsExactly ( expectedEnodeURL ) ;
}
@Test
@ -389,15 +371,15 @@ public class NodeLocalConfigPermissioningControllerTest {
. isInstanceOf ( RuntimeException . class )
. hasMessageContaining ( "Unable to read permissioning TOML config file" ) ;
assertThat ( controller . getNodesWhite list ( ) ) . containsExactly ( expectedEnodeURI ) ;
assertThat ( controller . getNodesAllow list ( ) ) . containsExactly ( expectedEnodeURI ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void whenAddingNodeShouldNotifyWhite listModifiedSubscribers ( ) {
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeWhite listUpdatedEvent expectedEvent =
new NodeWhite listUpdatedEvent (
public void whenAddingNodeShouldNotifyAllow listModifiedSubscribers ( ) {
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeAllow listUpdatedEvent expectedEvent =
new NodeAllow listUpdatedEvent (
Lists . newArrayList ( EnodeURL . fromString ( enode1 ) ) , Collections . emptyList ( ) ) ;
controller . subscribeToListUpdatedEvent ( consumer ) ;
@ -411,7 +393,7 @@ public class NodeLocalConfigPermissioningControllerTest {
public void whenAddingNodeDoesNotAddShouldNotNotifyAllowlistModifiedSubscribers ( ) {
// adding node before subscribing to allowlist modified events
controller . addNodes ( Lists . newArrayList ( enode1 ) ) ;
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
controller . subscribeToListUpdatedEvent ( consumer ) ;
// won't add duplicate node
@ -426,9 +408,9 @@ public class NodeLocalConfigPermissioningControllerTest {
// adding node before subscribing to allowlist modified events
controller . addNodes ( Lists . newArrayList ( enode1 ) ) ;
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeWhite listUpdatedEvent expectedEvent =
new NodeWhite listUpdatedEvent (
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeAllow listUpdatedEvent expectedEvent =
new NodeAllow listUpdatedEvent (
Collections . emptyList ( ) , Lists . newArrayList ( EnodeURL . fromString ( enode1 ) ) ) ;
controller . subscribeToListUpdatedEvent ( consumer ) ;
@ -440,7 +422,7 @@ public class NodeLocalConfigPermissioningControllerTest {
@Test
@SuppressWarnings ( "unchecked" )
public void whenRemovingNodeDoesNotRemoveShouldNotifyAllowlistModifiedSubscribers ( ) {
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
controller . subscribeToListUpdatedEvent ( consumer ) ;
// won't remove absent node
@ -451,17 +433,15 @@ public class NodeLocalConfigPermissioningControllerTest {
@Test
public void whenRemovingBootnodeShouldReturnRemoveBootnodeError ( ) {
NodeLocalConfigPermissioningController . NodesWhitelistResult expected =
new NodeLocalConfigPermissioningController . NodesWhitelistResult (
AllowlistOperationResult . ERROR_FIXED_NODE_CANNOT_BE_REMOVED ) ;
NodesAllowlistResult expected =
new NodesAllowlistResult ( AllowlistOperationResult . ERROR_FIXED_NODE_CANNOT_BE_REMOVED ) ;
bootnodesList . add ( EnodeURL . fromString ( enode1 ) ) ;
controller . addNodes ( Lists . newArrayList ( enode1 , enode2 ) ) ;
NodeLocalConfigPermissioningController . NodesWhitelistResult actualResult =
controller . removeNodes ( Lists . newArrayList ( enode1 ) ) ;
NodesAllowlistResult actualResult = controller . removeNodes ( Lists . newArrayList ( enode1 ) ) ;
assertThat ( actualResult ) . isEqualToComparingOnlyGivenFields ( expected , "result" ) ;
assertThat ( controller . getNodesWhite list ( ) ) . containsExactly ( enode1 , enode2 ) ;
assertThat ( controller . getNodesAllow list ( ) ) . containsExactly ( enode1 , enode2 ) ;
}
@Test
@ -470,9 +450,9 @@ public class NodeLocalConfigPermissioningControllerTest {
final Path permissionsFile = createPermissionsFileWithNode ( enode2 ) ;
final LocalPermissioningConfiguration permissioningConfig =
mock ( LocalPermissioningConfiguration . class ) ;
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeWhite listUpdatedEvent expectedEvent =
new NodeWhite listUpdatedEvent (
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final NodeAllow listUpdatedEvent expectedEvent =
new NodeAllow listUpdatedEvent (
Lists . newArrayList ( EnodeURL . fromString ( enode2 ) ) ,
Lists . newArrayList ( EnodeURL . fromString ( enode1 ) ) ) ;
@ -497,7 +477,7 @@ public class NodeLocalConfigPermissioningControllerTest {
final Path permissionsFile = createPermissionsFileWithNode ( enode1 ) ;
final LocalPermissioningConfiguration permissioningConfig =
mock ( LocalPermissioningConfiguration . class ) ;
final Consumer < NodeWhite listUpdatedEvent > consumer = mock ( Consumer . class ) ;
final Consumer < NodeAllow listUpdatedEvent > consumer = mock ( Consumer . class ) ;
when ( permissioningConfig . getNodePermissioningConfigFilePath ( ) )
. thenReturn ( permissionsFile . toAbsolutePath ( ) . toString ( ) ) ;