|
|
@ -27,6 +27,9 @@ import static org.mockito.Mockito.when; |
|
|
|
import static tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController.NodesWhitelistResult; |
|
|
|
import static tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController.NodesWhitelistResult; |
|
|
|
|
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.node.NodeWhitelistUpdatedEvent; |
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.node.NodeWhitelistUpdatedEvent; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.Counter; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.MetricCategory; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.metrics.MetricsSystem; |
|
|
|
import tech.pegasys.pantheon.util.enode.EnodeURL; |
|
|
|
import tech.pegasys.pantheon.util.enode.EnodeURL; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
@ -47,7 +50,7 @@ import org.junit.runner.RunWith; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.junit.MockitoJUnitRunner; |
|
|
|
import org.mockito.junit.MockitoJUnitRunner; |
|
|
|
|
|
|
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
@RunWith(MockitoJUnitRunner.StrictStubs.class) |
|
|
|
public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Mock private WhitelistPersistor whitelistPersistor; |
|
|
|
@Mock private WhitelistPersistor whitelistPersistor; |
|
|
@ -61,16 +64,40 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
private final EnodeURL selfEnode = |
|
|
|
private final EnodeURL selfEnode = |
|
|
|
EnodeURL.fromString( |
|
|
|
EnodeURL.fromString( |
|
|
|
"enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:1111"); |
|
|
|
"enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:1111"); |
|
|
|
|
|
|
|
@Mock private MetricsSystem metricsSystem; |
|
|
|
|
|
|
|
@Mock private Counter checkCounter; |
|
|
|
|
|
|
|
@Mock private Counter checkPermittedCounter; |
|
|
|
|
|
|
|
@Mock private Counter checkUnpermittedCounter; |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() { |
|
|
|
public void setUp() { |
|
|
|
bootnodesList.clear(); |
|
|
|
bootnodesList.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(metricsSystem.createCounter( |
|
|
|
|
|
|
|
MetricCategory.PERMISSIONING, |
|
|
|
|
|
|
|
"node_local_check_count", |
|
|
|
|
|
|
|
"Number of times the node local permissioning provider has been checked")) |
|
|
|
|
|
|
|
.thenReturn(checkCounter); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(metricsSystem.createCounter( |
|
|
|
|
|
|
|
MetricCategory.PERMISSIONING, |
|
|
|
|
|
|
|
"node_local_check_count_permitted", |
|
|
|
|
|
|
|
"Number of times the node local permissioning provider has been checked and returned permitted")) |
|
|
|
|
|
|
|
.thenReturn(checkPermittedCounter); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(metricsSystem.createCounter( |
|
|
|
|
|
|
|
MetricCategory.PERMISSIONING, |
|
|
|
|
|
|
|
"node_local_check_count_unpermitted", |
|
|
|
|
|
|
|
"Number of times the node local permissioning provider has been checked and returned unpermitted")) |
|
|
|
|
|
|
|
.thenReturn(checkUnpermittedCounter); |
|
|
|
|
|
|
|
|
|
|
|
controller = |
|
|
|
controller = |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
LocalPermissioningConfiguration.createDefault(), |
|
|
|
LocalPermissioningConfiguration.createDefault(), |
|
|
|
bootnodesList, |
|
|
|
bootnodesList, |
|
|
|
selfEnode.getNodeId(), |
|
|
|
selfEnode.getNodeId(), |
|
|
|
whitelistPersistor); |
|
|
|
whitelistPersistor, |
|
|
|
|
|
|
|
metricsSystem); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -165,8 +192,14 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
"enode://bbbb80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
"enode://bbbb80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
|
|
|
|
|
|
|
|
controller.addNodes(Arrays.asList(peer1)); |
|
|
|
controller.addNodes(Arrays.asList(peer1)); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyCountersUntouched(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(EnodeURL.fromString(peer2), EnodeURL.fromString(peer1))) |
|
|
|
|
|
|
|
.isFalse(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyCountersUnpermitted(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -261,7 +294,13 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenCheckingIfNodeIsPermittedOrderDoesNotMatter() { |
|
|
|
public void whenCheckingIfNodeIsPermittedOrderDoesNotMatter() { |
|
|
|
controller.addNodes(Arrays.asList(enode1)); |
|
|
|
controller.addNodes(Arrays.asList(enode1)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyCountersUntouched(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(EnodeURL.fromString(enode1), selfEnode)).isTrue(); |
|
|
|
assertThat(controller.isPermitted(EnodeURL.fromString(enode1), selfEnode)).isTrue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyCountersPermitted(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(selfEnode, EnodeURL.fromString(enode1))).isTrue(); |
|
|
|
assertThat(controller.isPermitted(selfEnode, EnodeURL.fromString(enode1))).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -302,7 +341,7 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
.thenReturn(Arrays.asList(URI.create(expectedEnodeURL))); |
|
|
|
.thenReturn(Arrays.asList(URI.create(expectedEnodeURL))); |
|
|
|
controller = |
|
|
|
controller = |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId()); |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId(), metricsSystem); |
|
|
|
|
|
|
|
|
|
|
|
controller.reload(); |
|
|
|
controller.reload(); |
|
|
|
|
|
|
|
|
|
|
@ -322,7 +361,7 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
.thenReturn(Arrays.asList(URI.create(expectedEnodeURI))); |
|
|
|
.thenReturn(Arrays.asList(URI.create(expectedEnodeURI))); |
|
|
|
controller = |
|
|
|
controller = |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId()); |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId(), metricsSystem); |
|
|
|
|
|
|
|
|
|
|
|
final Throwable thrown = catchThrowable(() -> controller.reload()); |
|
|
|
final Throwable thrown = catchThrowable(() -> controller.reload()); |
|
|
|
|
|
|
|
|
|
|
@ -421,7 +460,7 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1))); |
|
|
|
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1))); |
|
|
|
controller = |
|
|
|
controller = |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId()); |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId(), metricsSystem); |
|
|
|
controller.subscribeToListUpdatedEvent(consumer); |
|
|
|
controller.subscribeToListUpdatedEvent(consumer); |
|
|
|
|
|
|
|
|
|
|
|
controller.reload(); |
|
|
|
controller.reload(); |
|
|
@ -444,7 +483,7 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1))); |
|
|
|
when(permissioningConfig.getNodeWhitelist()).thenReturn(Arrays.asList(URI.create(enode1))); |
|
|
|
controller = |
|
|
|
controller = |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
new NodeLocalConfigPermissioningController( |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId()); |
|
|
|
permissioningConfig, bootnodesList, selfEnode.getNodeId(), metricsSystem); |
|
|
|
controller.subscribeToListUpdatedEvent(consumer); |
|
|
|
controller.subscribeToListUpdatedEvent(consumer); |
|
|
|
|
|
|
|
|
|
|
|
controller.reload(); |
|
|
|
controller.reload(); |
|
|
@ -459,4 +498,22 @@ public class NodeLocalConfigPermissioningControllerTest { |
|
|
|
Files.write(permissionsFile, nodePermissionsFileContent.getBytes(StandardCharsets.UTF_8)); |
|
|
|
Files.write(permissionsFile, nodePermissionsFileContent.getBytes(StandardCharsets.UTF_8)); |
|
|
|
return permissionsFile; |
|
|
|
return permissionsFile; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void verifyCountersUntouched() { |
|
|
|
|
|
|
|
verify(checkCounter, times(0)).inc(); |
|
|
|
|
|
|
|
verify(checkPermittedCounter, times(0)).inc(); |
|
|
|
|
|
|
|
verify(checkUnpermittedCounter, times(0)).inc(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void verifyCountersPermitted() { |
|
|
|
|
|
|
|
verify(checkCounter, times(1)).inc(); |
|
|
|
|
|
|
|
verify(checkPermittedCounter, times(1)).inc(); |
|
|
|
|
|
|
|
verify(checkUnpermittedCounter, times(0)).inc(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void verifyCountersUnpermitted() { |
|
|
|
|
|
|
|
verify(checkCounter, times(1)).inc(); |
|
|
|
|
|
|
|
verify(checkPermittedCounter, times(0)).inc(); |
|
|
|
|
|
|
|
verify(checkUnpermittedCounter, times(1)).inc(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|