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