mirror of https://github.com/hyperledger/besu
[NC-1938] Nodes whitelist JSON-RPC APIs (#476)
parent
c528ecf7a7
commit
637af126ca
@ -0,0 +1,34 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm.PermAddNodeTransaction; |
||||
|
||||
public class AddNodeSuccess implements Condition { |
||||
|
||||
private final PermAddNodeTransaction transaction; |
||||
|
||||
public AddNodeSuccess(final PermAddNodeTransaction transactions) { |
||||
this.transaction = transactions; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
final Boolean response = node.execute(transaction); |
||||
assertThat(response).isTrue(); |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm.PermGetNodesWhitelistTransaction; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class GetNodesWhitelistPopulated implements Condition { |
||||
|
||||
private final PermGetNodesWhitelistTransaction transaction; |
||||
private final int expectedNodeNum; |
||||
|
||||
public GetNodesWhitelistPopulated( |
||||
final PermGetNodesWhitelistTransaction transaction, final int expectedNodeNum) { |
||||
this.transaction = transaction; |
||||
this.expectedNodeNum = expectedNodeNum; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
final List<String> response = node.execute(transaction); |
||||
assertThat(response).isInstanceOf(List.class); |
||||
assertThat(response).size().isEqualTo(expectedNodeNum); |
||||
} |
||||
} |
@ -0,0 +1,34 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm.PermRemoveNodeTransaction; |
||||
|
||||
public class RemoveNodeSuccess implements Condition { |
||||
|
||||
private final PermRemoveNodeTransaction transaction; |
||||
|
||||
public RemoveNodeSuccess(final PermRemoveNodeTransaction transaction) { |
||||
this.transaction = transaction; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
final Boolean response = node.execute(transaction); |
||||
assertThat(response).isTrue(); |
||||
} |
||||
} |
@ -0,0 +1,41 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j.AddNodeResponse; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermAddNodeTransaction implements Transaction<Boolean> { |
||||
private final List<String> enodeList; |
||||
|
||||
public PermAddNodeTransaction(final List<String> enodeList) { |
||||
this.enodeList = enodeList; |
||||
} |
||||
|
||||
@Override |
||||
public Boolean execute(final PantheonWeb3j node) { |
||||
try { |
||||
final AddNodeResponse result = node.addNodesToWhitelist(enodeList).send(); |
||||
assertThat(result).isNotNull(); |
||||
return result.getResult(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j.GetNodesWhitelistResponse; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermGetNodesWhitelistTransaction implements Transaction<List<String>> { |
||||
@Override |
||||
public List<String> execute(final PantheonWeb3j node) { |
||||
try { |
||||
GetNodesWhitelistResponse result = node.getNodesWhitelist().send(); |
||||
assertThat(result).isNotNull(); |
||||
return result.getResult(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,41 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.PantheonWeb3j.RemoveNodeResponse; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermRemoveNodeTransaction implements Transaction<Boolean> { |
||||
private final List<String> enodeList; |
||||
|
||||
public PermRemoveNodeTransaction(final List<String> enodeList) { |
||||
this.enodeList = enodeList; |
||||
} |
||||
|
||||
@Override |
||||
public Boolean execute(final PantheonWeb3j node) { |
||||
try { |
||||
final RemoveNodeResponse result = node.removeNodesFromWhitelist(enodeList).send(); |
||||
assertThat(result).isNotNull(); |
||||
return result.getResult(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.jsonrpc.perm; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
|
||||
import org.assertj.core.util.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class PermAddNodesToWhitelistAcceptanceTest extends AcceptanceTestBase { |
||||
|
||||
private Node node; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
node = pantheon.createArchiveNodeWithRpcApis("node1", RpcApis.WEB3, RpcApis.NET, RpcApis.PERM); |
||||
cluster.start(node); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldAddSinglePeer() { |
||||
node.verify(perm.addNodesToWhitelist(Lists.newArrayList(enode1))); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldAddMultiplePeers() { |
||||
node.verify(perm.addNodesToWhitelist(Lists.newArrayList(enode1, enode2, enode3))); |
||||
} |
||||
} |
@ -0,0 +1,48 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.jsonrpc.perm; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
|
||||
import java.net.URI; |
||||
import java.util.ArrayList; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class PermGetNodesWhitelistAcceptanceTest extends AcceptanceTestBase { |
||||
private Node node; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.2:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.3:4567"; |
||||
private final ArrayList<URI> nodesWhitelist = |
||||
Lists.newArrayList(URI.create(enode1), URI.create(enode2), URI.create(enode3)); |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
|
||||
node = pantheon.createNodeWithNodesWhitelist("node1", nodesWhitelist); |
||||
cluster.start(node); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldGetNodesWhitelistContents() { |
||||
node.verify(perm.getNodesWhitelist(nodesWhitelist.size())); |
||||
} |
||||
} |
@ -0,0 +1,53 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.tests.acceptance.jsonrpc.perm; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
|
||||
import java.net.URI; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class PermRemoveNodesFromWhitelistAcceptanceTest extends AcceptanceTestBase { |
||||
|
||||
private Node node; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.1:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.2:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.3:4567"; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
node = |
||||
pantheon.createNodeWithNodesWhitelist( |
||||
"node1", |
||||
Lists.newArrayList(URI.create(enode1), URI.create(enode2), URI.create(enode3))); |
||||
cluster.start(node); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldRemoveSinglePeer() { |
||||
node.verify(perm.removeNodesFromWhitelist(Lists.newArrayList(enode1))); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldRemoveMultiplePeers() { |
||||
node.verify(perm.removeNodesFromWhitelist(Lists.newArrayList(enode1, enode2, enode3))); |
||||
} |
||||
} |
@ -0,0 +1,80 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.StringListParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
||||
import tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController; |
||||
|
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class PermAddNodesToWhitelist implements JsonRpcMethod { |
||||
|
||||
private final P2PNetwork p2pNetwork; |
||||
private final JsonRpcParameter parameters; |
||||
|
||||
public PermAddNodesToWhitelist(final P2PNetwork p2pNetwork, final JsonRpcParameter parameters) { |
||||
this.p2pNetwork = p2pNetwork; |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_addNodesToWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequest req) { |
||||
final StringListParameter enodeListParam = |
||||
parameters.required(req.getParams(), 0, StringListParameter.class); |
||||
|
||||
try { |
||||
List<DefaultPeer> peers = |
||||
enodeListParam |
||||
.getStringList() |
||||
.parallelStream() |
||||
.map(this::parsePeer) |
||||
.collect(Collectors.toList()); |
||||
|
||||
NodeWhitelistController.NodesWhitelistResult nodesWhitelistResult = |
||||
p2pNetwork.getNodeWhitelistController().addNodes(peers); |
||||
|
||||
switch (nodesWhitelistResult.result()) { |
||||
case SUCCESS: |
||||
return new JsonRpcSuccessResponse(req.getId(), true); |
||||
case ADD_ERROR_DUPLICATED_ENTRY: |
||||
return new JsonRpcErrorResponse( |
||||
req.getId(), JsonRpcError.NODE_WHITELIST_DUPLICATED_ENTRY); |
||||
default: |
||||
throw new Exception(); |
||||
} |
||||
} catch (IllegalArgumentException e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
} catch (Exception e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INTERNAL_ERROR); |
||||
} |
||||
} |
||||
|
||||
private DefaultPeer parsePeer(final String enodeURI) { |
||||
return DefaultPeer.fromURI(enodeURI); |
||||
} |
||||
} |
@ -0,0 +1,72 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.Endpoint; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; |
||||
|
||||
import java.util.List; |
||||
import java.util.OptionalInt; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.bouncycastle.util.encoders.Hex; |
||||
|
||||
public class PermGetNodesWhitelist implements JsonRpcMethod { |
||||
|
||||
private final P2PNetwork p2pNetwork; |
||||
|
||||
public PermGetNodesWhitelist(final P2PNetwork p2pNetwork) { |
||||
this.p2pNetwork = p2pNetwork; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_getNodesWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequest req) { |
||||
if (p2pNetwork.getNodeWhitelistController().nodeWhitelistSet()) { |
||||
List<Peer> nodesWhitelist = p2pNetwork.getNodeWhitelistController().getNodesWhitelist(); |
||||
|
||||
List<String> enodeList = |
||||
nodesWhitelist.parallelStream().map(this::buildEnodeURI).collect(Collectors.toList()); |
||||
|
||||
return new JsonRpcSuccessResponse(req.getId(), enodeList); |
||||
} else { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NODE_WHITELIST_NOT_SET); |
||||
} |
||||
} |
||||
|
||||
private String buildEnodeURI(final Peer s) { |
||||
String url = Hex.toHexString(s.getId().extractArray()); |
||||
Endpoint endpoint = s.getEndpoint(); |
||||
String nodeIp = endpoint.getHost(); |
||||
OptionalInt tcpPort = endpoint.getTcpPort(); |
||||
int udpPort = endpoint.getUdpPort(); |
||||
|
||||
if (tcpPort.isPresent() && (tcpPort.getAsInt() != udpPort)) { |
||||
return String.format( |
||||
"enode://%s@%s:%d?discport=%d", url, nodeIp, tcpPort.getAsInt(), udpPort); |
||||
} else { |
||||
return String.format("enode://%s@%s:%d", url, nodeIp, udpPort); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,80 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.StringListParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
||||
import tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController; |
||||
|
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class PermRemoveNodesFromWhitelist implements JsonRpcMethod { |
||||
|
||||
private final P2PNetwork p2pNetwork; |
||||
private final JsonRpcParameter parameters; |
||||
|
||||
public PermRemoveNodesFromWhitelist( |
||||
final P2PNetwork p2pNetwork, final JsonRpcParameter parameters) { |
||||
this.p2pNetwork = p2pNetwork; |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_removeNodesFromWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequest req) { |
||||
final StringListParameter enodeListParam = |
||||
parameters.required(req.getParams(), 0, StringListParameter.class); |
||||
|
||||
try { |
||||
List<DefaultPeer> peers = |
||||
enodeListParam |
||||
.getStringList() |
||||
.parallelStream() |
||||
.map(this::parsePeer) |
||||
.collect(Collectors.toList()); |
||||
|
||||
NodeWhitelistController.NodesWhitelistResult nodesWhitelistResult = |
||||
p2pNetwork.getNodeWhitelistController().removeNodes(peers); |
||||
|
||||
switch (nodesWhitelistResult.result()) { |
||||
case SUCCESS: |
||||
return new JsonRpcSuccessResponse(req.getId(), true); |
||||
case REMOVE_ERROR_ABSENT_ENTRY: |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NODE_WHITELIST_MISSING_ENTRY); |
||||
default: |
||||
throw new Exception(); |
||||
} |
||||
} catch (IllegalArgumentException e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
} catch (Exception e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INTERNAL_ERROR); |
||||
} |
||||
} |
||||
|
||||
private DefaultPeer parsePeer(final String enodeURI) { |
||||
return DefaultPeer.fromURI(enodeURI); |
||||
} |
||||
} |
@ -0,0 +1,34 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
||||
public class StringListParameter { |
||||
|
||||
private final List<String> stringList = new ArrayList<>(); |
||||
|
||||
@JsonCreator |
||||
public StringListParameter(final List<String> strings) { |
||||
if (strings != null) { |
||||
stringList.addAll(strings); |
||||
} |
||||
} |
||||
|
||||
public List<String> getStringList() { |
||||
return stringList; |
||||
} |
||||
} |
@ -0,0 +1,139 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
import static org.mockito.Mockito.verifyNoMoreInteractions; |
||||
import static org.mockito.Mockito.when; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResult; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResultType; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.assertj.core.util.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PermAddNodesToWhitelistTest { |
||||
|
||||
private static final boolean JSON_SUCCESS = true; |
||||
private PermAddNodesToWhitelist method; |
||||
private static final String METHOD_NAME = "perm_addNodesToWhitelist"; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String badEnode = "enod://dog@cat:fish"; |
||||
|
||||
@Mock private P2PNetwork p2pNetwork; |
||||
@Mock private NodeWhitelistController nodeWhitelistController; |
||||
|
||||
private JsonRpcParameter params = new JsonRpcParameter(); |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
method = new PermAddNodesToWhitelist(p2pNetwork, params); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnCorrectMethodName() { |
||||
assertThat(method.getName()).isEqualTo(METHOD_NAME); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowInvalidJsonRpcParametersExceptionWhenOnlyBadEnode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(badEnode)); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowInvalidJsonRpcParametersExceptionWhenBadEnodeInList() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(enode2, badEnode, enode1)); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowInvalidJsonRpcParametersExceptionWhenNoEnode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList("")); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldAddSingleValidNode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(enode1)); |
||||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getId(), JSON_SUCCESS); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.addNodes(any())) |
||||
.thenReturn(new NodesWhitelistResult(NodesWhitelistResultType.SUCCESS)); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).addNodes(any()); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldAddMultipleValidNodes() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(enode1, enode2, enode3)); |
||||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getId(), JSON_SUCCESS); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.addNodes(any())) |
||||
.thenReturn(new NodesWhitelistResult(NodesWhitelistResultType.SUCCESS)); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).addNodes(any()); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
private JsonRpcRequest buildRequest(final List<String> enodeList) { |
||||
return new JsonRpcRequest("2.0", METHOD_NAME, new Object[] {enodeList}); |
||||
} |
||||
} |
@ -0,0 +1,130 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
import static org.mockito.Mockito.verifyNoMoreInteractions; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; |
||||
import tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.assertj.core.util.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PermGetNodesWhitelistTest { |
||||
|
||||
private PermGetNodesWhitelist method; |
||||
private static final String METHOD_NAME = "perm_getNodesWhitelist"; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.11:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.12:4567"; |
||||
|
||||
@Mock private P2PNetwork p2pNetwork; |
||||
@Mock private NodeWhitelistController nodeWhitelistController; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
method = new PermGetNodesWhitelist(p2pNetwork); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnCorrectMethodName() { |
||||
assertThat(method.getName()).isEqualTo(METHOD_NAME); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnSuccessResponseWhenListPopulated() { |
||||
final JsonRpcRequest request = buildRequest(); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcSuccessResponse(request.getId(), Lists.newArrayList(enode1, enode2, enode3)); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.nodeWhitelistSet()).thenReturn(true); |
||||
when(nodeWhitelistController.getNodesWhitelist()) |
||||
.thenReturn(buildNodesList(enode1, enode2, enode3)); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).nodeWhitelistSet(); |
||||
verify(nodeWhitelistController, times(1)).getNodesWhitelist(); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnSuccessResponseWhenListSetAndEmpty() { |
||||
final JsonRpcRequest request = buildRequest(); |
||||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getId(), Lists.emptyList()); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.nodeWhitelistSet()).thenReturn(true); |
||||
when(nodeWhitelistController.getNodesWhitelist()).thenReturn(buildNodesList()); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).nodeWhitelistSet(); |
||||
verify(nodeWhitelistController, times(1)).getNodesWhitelist(); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnFailResponseWhenListNotSet() { |
||||
final JsonRpcRequest request = buildRequest(); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_NOT_SET); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.nodeWhitelistSet()).thenReturn(false); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).nodeWhitelistSet(); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
private JsonRpcRequest buildRequest() { |
||||
return new JsonRpcRequest("2.0", METHOD_NAME, new Object[] {}); |
||||
} |
||||
|
||||
private List<Peer> buildNodesList(final String... enodes) { |
||||
return Arrays.stream(enodes).parallel().map(DefaultPeer::fromURI).collect(Collectors.toList()); |
||||
} |
||||
} |
@ -0,0 +1,129 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
import static org.mockito.Mockito.verifyNoMoreInteractions; |
||||
import static org.mockito.Mockito.when; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResult; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResultType; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.assertj.core.util.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PermRemoveNodesFromWhitelistTest { |
||||
|
||||
private static final boolean SUCCESS_RESULT = true; |
||||
private PermRemoveNodesFromWhitelist method; |
||||
private static final String METHOD_NAME = "perm_removeNodesFromWhitelist"; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode3 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String badEnode = "enod://dog@cat:fish"; |
||||
|
||||
@Mock private P2PNetwork p2pNetwork; |
||||
@Mock NodeWhitelistController nodeWhitelistController; |
||||
|
||||
private JsonRpcParameter params = new JsonRpcParameter(); |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
method = new PermRemoveNodesFromWhitelist(p2pNetwork, params); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnCorrectMethodName() { |
||||
assertThat(method.getName()).isEqualTo(METHOD_NAME); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowInvalidJsonRpcParametersExceptionWhenBadEnode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(badEnode)); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowInvalidJsonRpcParametersExceptionWhenNoEnode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList("")); |
||||
final JsonRpcResponse expected = |
||||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NODE_WHITELIST_INVALID_ENTRY); |
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldRemoveSingleValidNode() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(enode1)); |
||||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getId(), SUCCESS_RESULT); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.removeNodes(any())) |
||||
.thenReturn(new NodesWhitelistResult(NodesWhitelistResultType.SUCCESS)); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).removeNodes(any()); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldRemoveMultipleValidNodes() { |
||||
final JsonRpcRequest request = buildRequest(Lists.newArrayList(enode1, enode2, enode3)); |
||||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getId(), SUCCESS_RESULT); |
||||
|
||||
when(p2pNetwork.getNodeWhitelistController()).thenReturn(nodeWhitelistController); |
||||
when(nodeWhitelistController.removeNodes(any())) |
||||
.thenReturn(new NodesWhitelistResult(NodesWhitelistResultType.SUCCESS)); |
||||
|
||||
final JsonRpcResponse actual = method.response(request); |
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); |
||||
|
||||
verify(nodeWhitelistController, times(1)).removeNodes(any()); |
||||
verifyNoMoreInteractions(nodeWhitelistController); |
||||
} |
||||
|
||||
private JsonRpcRequest buildRequest(final List<String> enodeList) { |
||||
return new JsonRpcRequest("2.0", METHOD_NAME, new Object[] {enodeList}); |
||||
} |
||||
} |
@ -0,0 +1,66 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* 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. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.p2p.permissioning; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResult; |
||||
import static tech.pegasys.pantheon.ethereum.p2p.permissioning.NodeWhitelistController.NodesWhitelistResultType; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class NodeWhitelistControllerTest { |
||||
|
||||
private NodeWhitelistController controller; |
||||
|
||||
private final String enode1 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
private final String enode2 = |
||||
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
controller = new NodeWhitelistController(new PermissioningConfiguration()); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldNotAddDuplicateNodes() { |
||||
controller.addNode(DefaultPeer.fromURI(enode1)); |
||||
|
||||
NodesWhitelistResult expected = |
||||
new NodesWhitelistResult(NodesWhitelistResultType.ADD_ERROR_DUPLICATED_ENTRY); |
||||
NodesWhitelistResult actualResult = |
||||
controller.addNodes( |
||||
Lists.newArrayList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode2))); |
||||
|
||||
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldNotRemoveNodesThatDoNotExist() { |
||||
NodesWhitelistResult expected = |
||||
new NodesWhitelistResult(NodesWhitelistResultType.REMOVE_ERROR_ABSENT_ENTRY); |
||||
NodesWhitelistResult actualResult = |
||||
controller.removeNodes( |
||||
Lists.newArrayList(DefaultPeer.fromURI(enode1), DefaultPeer.fromURI(enode2))); |
||||
|
||||
assertThat(actualResult).isEqualToComparingOnlyGivenFields(expected, "result"); |
||||
} |
||||
} |
Loading…
Reference in new issue