|
|
@ -10,7 +10,7 @@ |
|
|
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
|
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
|
|
* specific language governing permissions and limitations under the License. |
|
|
|
* specific language governing permissions and limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package tech.pegasys.pantheon.ethereum.p2p.permissioning; |
|
|
|
package tech.pegasys.pantheon.ethereum.permissioning; |
|
|
|
|
|
|
|
|
|
|
|
import static java.util.Collections.singletonList; |
|
|
|
import static java.util.Collections.singletonList; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
@ -22,15 +22,9 @@ import static org.mockito.Mockito.times; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
|
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResult; |
|
|
|
import static tech.pegasys.pantheon.ethereum.permissioning.NodeWhitelistController.NodesWhitelistResult; |
|
|
|
|
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
|
|
|
import tech.pegasys.pantheon.util.enode.EnodeURL; |
|
|
|
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.WhitelistFileSyncException; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.WhitelistOperationResult; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.ethereum.permissioning.WhitelistPersistor; |
|
|
|
|
|
|
|
import tech.pegasys.pantheon.util.bytes.BytesValue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URI; |
|
|
@ -67,13 +61,11 @@ public class NodeWhitelistControllerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenAddNodesInputHasExistingNodeShouldReturnAddErrorExistingEntry() { |
|
|
|
public void whenAddNodesInputHasExistingNodeShouldReturnAddErrorExistingEntry() { |
|
|
|
controller.addNode(DefaultPeer.fromURI(enode1)); |
|
|
|
controller.addNodes(Arrays.asList(enode1)); |
|
|
|
|
|
|
|
|
|
|
|
NodesWhitelistResult expected = |
|
|
|
NodesWhitelistResult expected = |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_EXISTING_ENTRY); |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_EXISTING_ENTRY); |
|
|
|
NodesWhitelistResult actualResult = |
|
|
|
NodesWhitelistResult actualResult = controller.addNodes(Lists.newArrayList(enode1, enode2)); |
|
|
|
controller.addNodes( |
|
|
|
|
|
|
|
Lists.newArrayList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode2))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
} |
|
|
|
} |
|
|
@ -83,9 +75,7 @@ public class NodeWhitelistControllerTest { |
|
|
|
NodesWhitelistResult expected = |
|
|
|
NodesWhitelistResult expected = |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_DUPLICATED_ENTRY); |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_DUPLICATED_ENTRY); |
|
|
|
|
|
|
|
|
|
|
|
NodesWhitelistResult actualResult = |
|
|
|
NodesWhitelistResult actualResult = controller.addNodes(Arrays.asList(enode1, enode1)); |
|
|
|
controller.addNodes( |
|
|
|
|
|
|
|
Arrays.asList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode1))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
} |
|
|
|
} |
|
|
@ -112,9 +102,7 @@ public class NodeWhitelistControllerTest { |
|
|
|
public void whenRemoveNodesInputHasAbsentNodeShouldReturnRemoveErrorAbsentEntry() { |
|
|
|
public void whenRemoveNodesInputHasAbsentNodeShouldReturnRemoveErrorAbsentEntry() { |
|
|
|
NodesWhitelistResult expected = |
|
|
|
NodesWhitelistResult expected = |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_ABSENT_ENTRY); |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_ABSENT_ENTRY); |
|
|
|
NodesWhitelistResult actualResult = |
|
|
|
NodesWhitelistResult actualResult = controller.removeNodes(Lists.newArrayList(enode1, enode2)); |
|
|
|
controller.removeNodes( |
|
|
|
|
|
|
|
Lists.newArrayList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode2))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
} |
|
|
|
} |
|
|
@ -123,9 +111,7 @@ public class NodeWhitelistControllerTest { |
|
|
|
public void whenRemoveNodesInputHasDuplicateNodesShouldReturnErrorDuplicatedEntry() { |
|
|
|
public void whenRemoveNodesInputHasDuplicateNodesShouldReturnErrorDuplicatedEntry() { |
|
|
|
NodesWhitelistResult expected = |
|
|
|
NodesWhitelistResult expected = |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_DUPLICATED_ENTRY); |
|
|
|
new NodesWhitelistResult(WhitelistOperationResult.ERROR_DUPLICATED_ENTRY); |
|
|
|
NodesWhitelistResult actualResult = |
|
|
|
NodesWhitelistResult actualResult = controller.removeNodes(Lists.newArrayList(enode1, enode1)); |
|
|
|
controller.removeNodes( |
|
|
|
|
|
|
|
Lists.newArrayList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode1))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
|
|
|
} |
|
|
|
} |
|
|
@ -150,103 +136,60 @@ public class NodeWhitelistControllerTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenNodeIdsAreDifferentItShouldNotBePermitted() { |
|
|
|
public void whenNodeIdsAreDifferentItShouldNotBePermitted() { |
|
|
|
Peer peer1 = |
|
|
|
String peer1 = |
|
|
|
new DefaultPeer( |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
BytesValue.fromHexString( |
|
|
|
String peer2 = |
|
|
|
"0xaaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
"enode://bbbb80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303); |
|
|
|
controller.addNodes(Arrays.asList(peer1)); |
|
|
|
Peer peer2 = |
|
|
|
|
|
|
|
new DefaultPeer( |
|
|
|
|
|
|
|
BytesValue.fromHexString( |
|
|
|
|
|
|
|
"0xbbba80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
|
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.addNode(peer1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenNodesHostsAreDifferentItShouldNotBePermitted() { |
|
|
|
public void whenNodesHostsAreDifferentItShouldNotBePermitted() { |
|
|
|
Peer peer1 = |
|
|
|
String peer1 = |
|
|
|
new DefaultPeer( |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
BytesValue.fromHexString( |
|
|
|
String peer2 = |
|
|
|
"0xaaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.2:30303"; |
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303); |
|
|
|
controller.addNodes(Arrays.asList(peer1)); |
|
|
|
Peer peer2 = |
|
|
|
|
|
|
|
new DefaultPeer( |
|
|
|
|
|
|
|
BytesValue.fromHexString( |
|
|
|
|
|
|
|
"0xaaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
|
|
|
|
"127.0.0.2", |
|
|
|
|
|
|
|
30303); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.addNode(peer1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenNodesUdpPortsAreDifferentItShouldNotBePermitted() { |
|
|
|
public void whenNodesUdpPortsAreDifferentItShouldNotBePermitted() { |
|
|
|
Peer peer1 = |
|
|
|
String peer1 = |
|
|
|
new DefaultPeer( |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30301"; |
|
|
|
BytesValue.fromHexString( |
|
|
|
String peer2 = |
|
|
|
"0xaaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30302"; |
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30301); |
|
|
|
controller.addNodes(Arrays.asList(peer1)); |
|
|
|
Peer peer2 = |
|
|
|
|
|
|
|
new DefaultPeer( |
|
|
|
|
|
|
|
BytesValue.fromHexString( |
|
|
|
|
|
|
|
"0xaaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
|
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30302); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.addNode(peer1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
assertThat(controller.isPermitted(peer2)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenCheckingIfNodeIsPermittedTcpPortShouldNotBeConsideredIfAbsent() { |
|
|
|
public void whenCheckingIfNodeIsPermittedTcpPortShouldNotBeConsideredIfAbsent() { |
|
|
|
Peer peerWithTcpPortSet = |
|
|
|
String peerWithTcpPortSet = |
|
|
|
new DefaultPeer( |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303?discport=10001"; |
|
|
|
BytesValue.fromHexString( |
|
|
|
String peerWithoutTcpPortSet = |
|
|
|
"0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303"; |
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303, |
|
|
|
controller.addNodes(Arrays.asList(peerWithTcpPortSet)); |
|
|
|
10001); |
|
|
|
|
|
|
|
Peer peerWithoutTcpPortSet = |
|
|
|
assertThat(controller.isPermitted(peerWithoutTcpPortSet)).isTrue(); |
|
|
|
new DefaultPeer( |
|
|
|
|
|
|
|
BytesValue.fromHexString( |
|
|
|
|
|
|
|
"0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
|
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.addNode(peerWithoutTcpPortSet); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peerWithTcpPortSet)).isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void whenCheckingIfNodeIsPermittedTcpPortShouldBeConsideredIfPresent() { |
|
|
|
public void whenCheckingIfNodeIsPermittedTcpPortShouldBeConsideredIfPresent() { |
|
|
|
Peer peerWithTcpPortSet = |
|
|
|
String peerWithTcpPortSet = |
|
|
|
new DefaultPeer( |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303?discport=10001"; |
|
|
|
BytesValue.fromHexString( |
|
|
|
String peerWithDifferentTcpPortSet = |
|
|
|
"0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
"enode://aaaa80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@127.0.0.1:30303?discport=10002"; |
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303, |
|
|
|
controller.addNodes(Arrays.asList(peerWithDifferentTcpPortSet)); |
|
|
|
10001); |
|
|
|
|
|
|
|
Peer peerWithDifferentTcpPortSet = |
|
|
|
|
|
|
|
new DefaultPeer( |
|
|
|
|
|
|
|
BytesValue.fromHexString( |
|
|
|
|
|
|
|
"0x6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0"), |
|
|
|
|
|
|
|
"127.0.0.1", |
|
|
|
|
|
|
|
30303, |
|
|
|
|
|
|
|
10002); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.addNode(peerWithDifferentTcpPortSet); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.isPermitted(peerWithTcpPortSet)).isFalse(); |
|
|
|
assertThat(controller.isPermitted(peerWithTcpPortSet)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
@ -254,8 +197,8 @@ public class NodeWhitelistControllerTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void stateShouldRevertIfWhitelistPersistFails() |
|
|
|
public void stateShouldRevertIfWhitelistPersistFails() |
|
|
|
throws IOException, WhitelistFileSyncException { |
|
|
|
throws IOException, WhitelistFileSyncException { |
|
|
|
List<Peer> newNode1 = singletonList(DefaultPeer.fromURI(enode1)); |
|
|
|
List<String> newNode1 = singletonList(new EnodeURL(enode1).toString()); |
|
|
|
List<Peer> newNode2 = singletonList(DefaultPeer.fromURI(enode2)); |
|
|
|
List<String> newNode2 = singletonList(new EnodeURL(enode2).toString()); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.getNodesWhitelist().size()).isEqualTo(0); |
|
|
|
assertThat(controller.getNodesWhitelist().size()).isEqualTo(0); |
|
|
|
|
|
|
|
|
|
|
@ -289,8 +232,7 @@ public class NodeWhitelistControllerTest { |
|
|
|
|
|
|
|
|
|
|
|
controller.reload(); |
|
|
|
controller.reload(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.getNodesWhitelist()) |
|
|
|
assertThat(controller.getNodesWhitelist()).containsExactly(expectedEnodeURL); |
|
|
|
.containsExactly(DefaultPeer.fromURI(expectedEnodeURL)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -311,8 +253,7 @@ public class NodeWhitelistControllerTest { |
|
|
|
.isInstanceOf(RuntimeException.class) |
|
|
|
.isInstanceOf(RuntimeException.class) |
|
|
|
.hasMessageContaining("Unable to read permissions TOML config file"); |
|
|
|
.hasMessageContaining("Unable to read permissions TOML config file"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(controller.getNodesWhitelist()) |
|
|
|
assertThat(controller.getNodesWhitelist()).containsExactly(expectedEnodeURI); |
|
|
|
.containsExactly(DefaultPeer.fromURI(expectedEnodeURI)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Path createPermissionsFileWithNode(final String node) throws IOException { |
|
|
|
private Path createPermissionsFileWithNode(final String node) throws IOException { |