mirror of https://github.com/hyperledger/besu
Acc whitelist api (#487)
* Implemented list/add/remove accounts from whitelist * Including account whitelist methods in the JSON-RPC API * Fixing json rpc response for eth_sendrawTransaction with account not authorized * Refactoring TransactionPool account whitelist logic * Acceptance test for accounts whitelist * Errorprone * Fixed account nonce tracking in ATs
parent
8d4048b988
commit
0a8f6cbddf
@ -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.condition.eth; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.catchThrowable; |
||||
|
||||
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.eth.EthSendRawTransactionTransaction; |
||||
|
||||
import org.web3j.protocol.exceptions.ClientConnectionException; |
||||
|
||||
public class ExpectEthSendRawTransactionException implements Condition { |
||||
|
||||
private final EthSendRawTransactionTransaction transaction; |
||||
private final String expectedMessage; |
||||
|
||||
public ExpectEthSendRawTransactionException( |
||||
final EthSendRawTransactionTransaction transaction, final String expectedMessage) { |
||||
this.transaction = transaction; |
||||
this.expectedMessage = expectedMessage; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
final Throwable thrown = catchThrowable(() -> node.execute(transaction)); |
||||
assertThat(thrown).isInstanceOf(ClientConnectionException.class); |
||||
assertThat(thrown.getMessage()).contains(expectedMessage); |
||||
} |
||||
} |
@ -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.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.PermAddAccountsToWhitelistTransaction; |
||||
|
||||
public class AddAccountsToWhitelistSuccessfully implements Condition { |
||||
|
||||
private final PermAddAccountsToWhitelistTransaction transaction; |
||||
|
||||
public AddAccountsToWhitelistSuccessfully( |
||||
final PermAddAccountsToWhitelistTransaction transaction) { |
||||
this.transaction = transaction; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
Boolean result = node.execute(transaction); |
||||
assertThat(result).isTrue(); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
/* |
||||
* 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.PermGetAccountsWhitelistTransaction; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class GetExpectedAccountsWhitelist implements Condition { |
||||
|
||||
private final PermGetAccountsWhitelistTransaction transaction; |
||||
private final List<String> expectedList; |
||||
|
||||
public GetExpectedAccountsWhitelist( |
||||
final PermGetAccountsWhitelistTransaction transaction, final List<String> expectedList) { |
||||
this.transaction = transaction; |
||||
this.expectedList = expectedList; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
List<String> accounts = node.execute(transaction); |
||||
assertThat(accounts).containsExactlyInAnyOrder(expectedList.toArray(new String[] {})); |
||||
} |
||||
} |
@ -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.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.PermRemoveAccountsFromWhitelistTransaction; |
||||
|
||||
public class RemoveAccountsFromWhitelistSuccessfully implements Condition { |
||||
|
||||
private final PermRemoveAccountsFromWhitelistTransaction transaction; |
||||
|
||||
public RemoveAccountsFromWhitelistSuccessfully( |
||||
final PermRemoveAccountsFromWhitelistTransaction transaction) { |
||||
this.transaction = transaction; |
||||
} |
||||
|
||||
@Override |
||||
public void verify(final Node node) { |
||||
Boolean result = node.execute(transaction); |
||||
assertThat(result).isTrue(); |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* 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.jsonrpc; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.AddAccountsToWhitelistSuccessfully; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.GetExpectedAccountsWhitelist; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.RemoveAccountsFromWhitelistSuccessfully; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transactions; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
public class Perm { |
||||
|
||||
public Perm(final Transactions transactions) { |
||||
this.transactions = transactions; |
||||
} |
||||
|
||||
private final Transactions transactions; |
||||
|
||||
public Condition addAccountsToWhitelist(final String... accounts) { |
||||
return new AddAccountsToWhitelistSuccessfully(transactions.addAccountsToWhitelist(accounts)); |
||||
} |
||||
|
||||
public Condition removeAccountsFromWhitelist(final String... accounts) { |
||||
return new RemoveAccountsFromWhitelistSuccessfully( |
||||
transactions.removeAccountsFromWhitelist(accounts)); |
||||
} |
||||
|
||||
public Condition expectAccountsWhitelist(final String... expectedAccounts) { |
||||
return new GetExpectedAccountsWhitelist( |
||||
transactions.getAccountsWhiteList(), Arrays.asList(expectedAccounts)); |
||||
} |
||||
} |
@ -0,0 +1,45 @@ |
||||
/* |
||||
* 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.eth; |
||||
|
||||
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.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.math.BigInteger; |
||||
|
||||
import org.web3j.protocol.core.DefaultBlockParameterName; |
||||
import org.web3j.protocol.core.methods.response.EthGetTransactionCount; |
||||
|
||||
public class EthGetTransactionCountTransaction implements Transaction<BigInteger> { |
||||
|
||||
private final String accountAddress; |
||||
|
||||
public EthGetTransactionCountTransaction(final String accountAddress) { |
||||
this.accountAddress = accountAddress; |
||||
} |
||||
|
||||
@Override |
||||
public BigInteger execute(final PantheonWeb3j node) { |
||||
try { |
||||
EthGetTransactionCount result = |
||||
node.ethGetTransactionCount(accountAddress, DefaultBlockParameterName.LATEST).send(); |
||||
assertThat(result).isNotNull(); |
||||
return result.getTransactionCount(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
/* |
||||
* 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.eth; |
||||
|
||||
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.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import org.web3j.protocol.core.methods.response.EthSendTransaction; |
||||
|
||||
public class EthSendRawTransactionTransaction implements Transaction<String> { |
||||
|
||||
private final String transactionData; |
||||
|
||||
EthSendRawTransactionTransaction(final String transactionData) { |
||||
this.transactionData = transactionData; |
||||
} |
||||
|
||||
@Override |
||||
public String execute(final PantheonWeb3j node) { |
||||
try { |
||||
EthSendTransaction response = node.ethSendRawTransaction(transactionData).send(); |
||||
assertThat(response.getTransactionHash()).isNotNull(); |
||||
return response.getTransactionHash(); |
||||
} catch (final IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
/* |
||||
* 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.AddAccountsToWhitelistResponse; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermAddAccountsToWhitelistTransaction implements Transaction<Boolean> { |
||||
|
||||
private final List<String> accounts; |
||||
|
||||
public PermAddAccountsToWhitelistTransaction(final List<String> accounts) { |
||||
this.accounts = accounts; |
||||
} |
||||
|
||||
@Override |
||||
public Boolean execute(final PantheonWeb3j node) { |
||||
try { |
||||
AddAccountsToWhitelistResponse response = node.addAccountsToWhitelist(accounts).send(); |
||||
assertThat(response.getResult()).isTrue(); |
||||
return response.getResult(); |
||||
} catch (IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
/* |
||||
* 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.GetAccountsWhitelistResponse; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermGetAccountsWhitelistTransaction implements Transaction<List<String>> { |
||||
|
||||
@Override |
||||
public List<String> execute(final PantheonWeb3j node) { |
||||
try { |
||||
GetAccountsWhitelistResponse response = node.getAccountsWhitelist().send(); |
||||
assertThat(response.getResult()).isNotNull(); |
||||
return response.getResult(); |
||||
} catch (IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
/* |
||||
* 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.RemoveAccountsFromWhitelistResponse; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
public class PermRemoveAccountsFromWhitelistTransaction implements Transaction<Boolean> { |
||||
|
||||
private final List<String> accounts; |
||||
|
||||
public PermRemoveAccountsFromWhitelistTransaction(final List<String> accounts) { |
||||
this.accounts = accounts; |
||||
} |
||||
|
||||
@Override |
||||
public Boolean execute(final PantheonWeb3j node) { |
||||
try { |
||||
RemoveAccountsFromWhitelistResponse response = |
||||
node.removeAccountsFromWhitelist(accounts).send(); |
||||
assertThat(response.getResult()).isTrue(); |
||||
return response.getResult(); |
||||
} catch (IOException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
/* |
||||
* 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.permissioning; |
||||
|
||||
import static org.web3j.utils.Convert.Unit.ETHER; |
||||
|
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; |
||||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTransaction; |
||||
|
||||
import java.math.BigInteger; |
||||
import java.util.Arrays; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
public class AccountsWhitelistAcceptanceTest extends AcceptanceTestBase { |
||||
|
||||
private Node node; |
||||
private Account senderA; |
||||
private Account senderB; |
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
senderA = accounts.getPrimaryBenefactor(); |
||||
senderB = accounts.getSecondaryBenefactor(); |
||||
|
||||
node = pantheon.createNodeWithAccountsWhitelist("node", Arrays.asList(senderA.getAddress())); |
||||
cluster.start(node); |
||||
} |
||||
|
||||
@Test |
||||
public void onlyAllowedAccountCanSubmitTransactions() { |
||||
Account beneficiary = accounts.createAccount("beneficiary"); |
||||
|
||||
node.execute(transactions.createTransfer(senderA, beneficiary, 1)); |
||||
node.verify(beneficiary.balanceEquals("1", ETHER)); |
||||
|
||||
verifyTransferForbidden(senderB, beneficiary); |
||||
} |
||||
|
||||
@Test |
||||
public void manipulatingAccountsWhitelistViaJsonRpc() { |
||||
Account beneficiary = accounts.createAccount("beneficiary"); |
||||
node.verify(beneficiary.balanceEquals("0", ETHER)); |
||||
|
||||
verifyTransferForbidden(senderB, beneficiary); |
||||
|
||||
node.execute(transactions.addAccountsToWhitelist(senderB.getAddress())); |
||||
node.verify(perm.expectAccountsWhitelist(senderA.getAddress(), senderB.getAddress())); |
||||
|
||||
node.execute(transactions.createTransfer(senderB, beneficiary, 1)); |
||||
node.verify(beneficiary.balanceEquals("1", ETHER)); |
||||
|
||||
node.execute(transactions.removeAccountsFromWhitelist(senderB.getAddress())); |
||||
node.verify(perm.expectAccountsWhitelist(senderA.getAddress())); |
||||
verifyTransferForbidden(senderB, beneficiary); |
||||
} |
||||
|
||||
private void verifyTransferForbidden(final Account sender, final Account beneficiary) { |
||||
BigInteger nonce = node.execute(transactions.getTransactionCount(sender.getAddress())); |
||||
TransferTransaction transfer = transactions.createTransfer(sender, beneficiary, 1, nonce); |
||||
node.verify( |
||||
eth.sendRawTransactionExceptional( |
||||
transfer.signedTransactionData(), |
||||
"Sender account not authorized to send transactions")); |
||||
} |
||||
} |
@ -0,0 +1,62 @@ |
||||
/* |
||||
* 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.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.permissioning.AccountWhitelistController; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.AddResult; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class PermAddAccountsToWhitelist implements JsonRpcMethod { |
||||
|
||||
private final JsonRpcParameter parameters; |
||||
private final AccountWhitelistController whitelistController; |
||||
|
||||
public PermAddAccountsToWhitelist( |
||||
final AccountWhitelistController whitelistController, final JsonRpcParameter parameters) { |
||||
this.whitelistController = whitelistController; |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_addAccountsToWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public JsonRpcResponse response(final JsonRpcRequest request) { |
||||
final List<String> accountsList = parameters.required(request.getParams(), 0, List.class); |
||||
final AddResult addResult = whitelistController.addAccounts(accountsList); |
||||
|
||||
switch (addResult) { |
||||
case ERROR_INVALID_ENTRY: |
||||
return new JsonRpcErrorResponse( |
||||
request.getId(), JsonRpcError.ACCOUNT_WHITELIST_INVALID_ENTRY); |
||||
case ERROR_DUPLICATED_ENTRY: |
||||
return new JsonRpcErrorResponse( |
||||
request.getId(), JsonRpcError.ACCOUNT_WHITELIST_DUPLICATED_ENTRY); |
||||
case SUCCESS: |
||||
return new JsonRpcSuccessResponse(request.getId(), true); |
||||
default: |
||||
throw new IllegalStateException("Unmapped result from AccountWhitelistController"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* 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.permissioning.AccountWhitelistController; |
||||
|
||||
public class PermGetAccountsWhitelist implements JsonRpcMethod { |
||||
|
||||
private final AccountWhitelistController whitelistController; |
||||
|
||||
public PermGetAccountsWhitelist(final AccountWhitelistController whitelistController) { |
||||
this.whitelistController = whitelistController; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_getAccountsWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequest request) { |
||||
if (!whitelistController.isAccountWhiteListSet()) { |
||||
return new JsonRpcErrorResponse(request.getId(), JsonRpcError.ACCOUNT_WHITELIST_NOT_SET); |
||||
} else { |
||||
return new JsonRpcSuccessResponse(request.getId(), whitelistController.getAccountWhitelist()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,62 @@ |
||||
/* |
||||
* 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.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.permissioning.AccountWhitelistController; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.RemoveResult; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class PermRemoveAccountsFromWhitelist implements JsonRpcMethod { |
||||
|
||||
private final JsonRpcParameter parameters; |
||||
private final AccountWhitelistController whitelistController; |
||||
|
||||
public PermRemoveAccountsFromWhitelist( |
||||
final AccountWhitelistController whitelistController, final JsonRpcParameter parameters) { |
||||
this.whitelistController = whitelistController; |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "perm_removeAccountsFromWhitelist"; |
||||
} |
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public JsonRpcResponse response(final JsonRpcRequest request) { |
||||
final List<String> accountsList = parameters.required(request.getParams(), 0, List.class); |
||||
final RemoveResult removeResult = whitelistController.removeAccounts(accountsList); |
||||
|
||||
switch (removeResult) { |
||||
case ERROR_INVALID_ENTRY: |
||||
return new JsonRpcErrorResponse( |
||||
request.getId(), JsonRpcError.ACCOUNT_WHITELIST_INVALID_ENTRY); |
||||
case ERROR_ABSENT_ENTRY: |
||||
return new JsonRpcErrorResponse( |
||||
request.getId(), JsonRpcError.ACCOUNT_WHITELIST_ABSENT_ENTRY); |
||||
case SUCCESS: |
||||
return new JsonRpcSuccessResponse(request.getId(), true); |
||||
default: |
||||
throw new IllegalStateException("Unmapped result from AccountWhitelistController"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,105 @@ |
||||
/* |
||||
* 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.assertj.core.api.Assertions.catchThrowable; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.exception.InvalidJsonRpcParameters; |
||||
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.permissioning.AccountWhitelistController; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.AddResult; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
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 PermAddAccountsToWhitelistTest { |
||||
|
||||
@Mock private AccountWhitelistController accountWhitelist; |
||||
private PermAddAccountsToWhitelist method; |
||||
|
||||
@Before |
||||
public void before() { |
||||
method = new PermAddAccountsToWhitelist(accountWhitelist, new JsonRpcParameter()); |
||||
} |
||||
|
||||
@Test |
||||
public void getNameShouldReturnExpectedName() { |
||||
assertThat(method.getName()).isEqualTo("perm_addAccountsToWhitelist"); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountsAreAddedToWhitelistShouldReturnTrue() { |
||||
List<String> accounts = Arrays.asList("0x0", "0x1"); |
||||
JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, true); |
||||
when(accountWhitelist.addAccounts(eq(accounts))).thenReturn(AddResult.SUCCESS); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(accounts)); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountIsInvalidShouldReturnInvalidAccountErrorResponse() { |
||||
JsonRpcResponse expectedResponse = |
||||
new JsonRpcErrorResponse(null, JsonRpcError.ACCOUNT_WHITELIST_INVALID_ENTRY); |
||||
when(accountWhitelist.addAccounts(any())).thenReturn(AddResult.ERROR_INVALID_ENTRY); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(new ArrayList<>())); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountIsDuplicatedShouldReturnDuplicatedAccountErrorResponse() { |
||||
JsonRpcResponse expectedResponse = |
||||
new JsonRpcErrorResponse(null, JsonRpcError.ACCOUNT_WHITELIST_DUPLICATED_ENTRY); |
||||
when(accountWhitelist.addAccounts(any())).thenReturn(AddResult.ERROR_DUPLICATED_ENTRY); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(new ArrayList<>())); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenEmptyParamOnRequestShouldThrowInvalidJsonRpcException() { |
||||
JsonRpcRequest request = |
||||
new JsonRpcRequest("2.0", "perm_addAccountsToWhitelist", new Object[] {}); |
||||
|
||||
final Throwable thrown = catchThrowable(() -> method.response(request)); |
||||
assertThat(thrown) |
||||
.hasNoCause() |
||||
.isInstanceOf(InvalidJsonRpcParameters.class) |
||||
.hasMessage("Missing required json rpc parameter at index 0"); |
||||
} |
||||
|
||||
private JsonRpcRequest request(final List<String> accounts) { |
||||
return new JsonRpcRequest("2.0", "perm_addAccountsToWhitelist", new Object[] {accounts}); |
||||
} |
||||
} |
@ -0,0 +1,88 @@ |
||||
/* |
||||
* 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.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.permissioning.AccountWhitelistController; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
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 PermGetAccountsWhitelistTest { |
||||
|
||||
private static final JsonRpcRequest request = |
||||
new JsonRpcRequest("2.0", "perm_getAccountsWhitelist", null); |
||||
|
||||
@Mock private AccountWhitelistController accountWhitelist; |
||||
private PermGetAccountsWhitelist method; |
||||
|
||||
@Before |
||||
public void before() { |
||||
method = new PermGetAccountsWhitelist(accountWhitelist); |
||||
} |
||||
|
||||
@Test |
||||
public void getNameShouldReturnExpectedName() { |
||||
assertThat(method.getName()).isEqualTo("perm_getAccountsWhitelist"); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnExpectedListOfAccountsWhenWhitelistHasBeenSet() { |
||||
List<String> accountsList = Arrays.asList("0x0", "0x1"); |
||||
when(accountWhitelist.isAccountWhiteListSet()).thenReturn(true); |
||||
when(accountWhitelist.getAccountWhitelist()).thenReturn(accountsList); |
||||
JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, accountsList); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnEmptyListOfAccountsWhenWhitelistHasBeenSetAndIsEmpty() { |
||||
List<String> emptyAccountsList = new ArrayList<>(); |
||||
when(accountWhitelist.isAccountWhiteListSet()).thenReturn(true); |
||||
when(accountWhitelist.getAccountWhitelist()).thenReturn(emptyAccountsList); |
||||
JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, emptyAccountsList); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldReturnErrorWhenWhitelistHasNotBeenSet() { |
||||
when(accountWhitelist.isAccountWhiteListSet()).thenReturn(false); |
||||
JsonRpcResponse expectedResponse = |
||||
new JsonRpcErrorResponse(null, JsonRpcError.ACCOUNT_WHITELIST_NOT_SET); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
} |
@ -0,0 +1,105 @@ |
||||
/* |
||||
* 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.assertj.core.api.Assertions.catchThrowable; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.exception.InvalidJsonRpcParameters; |
||||
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.permissioning.AccountWhitelistController; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.RemoveResult; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
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 PermRemoveAccountsFromWhitelistTest { |
||||
|
||||
@Mock private AccountWhitelistController accountWhitelist; |
||||
private PermRemoveAccountsFromWhitelist method; |
||||
|
||||
@Before |
||||
public void before() { |
||||
method = new PermRemoveAccountsFromWhitelist(accountWhitelist, new JsonRpcParameter()); |
||||
} |
||||
|
||||
@Test |
||||
public void getNameShouldReturnExpectedName() { |
||||
assertThat(method.getName()).isEqualTo("perm_removeAccountsFromWhitelist"); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountsAreRemovedFromWhitelistShouldReturnTrue() { |
||||
List<String> accounts = Arrays.asList("0x0", "0x1"); |
||||
JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, true); |
||||
when(accountWhitelist.removeAccounts(eq(accounts))).thenReturn(RemoveResult.SUCCESS); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(accounts)); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountIsInvalidShouldReturnInvalidAccountErrorResponse() { |
||||
JsonRpcResponse expectedResponse = |
||||
new JsonRpcErrorResponse(null, JsonRpcError.ACCOUNT_WHITELIST_INVALID_ENTRY); |
||||
when(accountWhitelist.removeAccounts(any())).thenReturn(RemoveResult.ERROR_INVALID_ENTRY); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(new ArrayList<>())); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountIsAbsentShouldReturnAbsentAccountErrorResponse() { |
||||
JsonRpcResponse expectedResponse = |
||||
new JsonRpcErrorResponse(null, JsonRpcError.ACCOUNT_WHITELIST_ABSENT_ENTRY); |
||||
when(accountWhitelist.removeAccounts(any())).thenReturn(RemoveResult.ERROR_ABSENT_ENTRY); |
||||
|
||||
JsonRpcResponse actualResponse = method.response(request(new ArrayList<>())); |
||||
|
||||
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||
} |
||||
|
||||
@Test |
||||
public void whenEmptyParamOnRequestShouldThrowInvalidJsonRpcException() { |
||||
JsonRpcRequest request = |
||||
new JsonRpcRequest("2.0", "perm_removeAccountsFromWhitelist", new Object[] {}); |
||||
|
||||
final Throwable thrown = catchThrowable(() -> method.response(request)); |
||||
assertThat(thrown) |
||||
.hasNoCause() |
||||
.isInstanceOf(InvalidJsonRpcParameters.class) |
||||
.hasMessage("Missing required json rpc parameter at index 0"); |
||||
} |
||||
|
||||
private JsonRpcRequest request(final List<String> accounts) { |
||||
return new JsonRpcRequest("2.0", "perm_removeAccountsFromWhitelist", new Object[] {accounts}); |
||||
} |
||||
} |
@ -0,0 +1,142 @@ |
||||
/* |
||||
* 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.permissioning; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.AddResult; |
||||
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController.RemoveResult; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
|
||||
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 AccountWhitelistControllerTest { |
||||
|
||||
private AccountWhitelistController controller; |
||||
@Mock private PermissioningConfiguration permissioningConfig; |
||||
|
||||
@Before |
||||
public void before() { |
||||
controller = new AccountWhitelistController(permissioningConfig); |
||||
} |
||||
|
||||
@Test |
||||
public void newInstanceWithNullPermConfigShouldHaveAccountWhitelistNotSet() { |
||||
controller = new AccountWhitelistController(null); |
||||
|
||||
assertThat(controller.isAccountWhiteListSet()).isFalse(); |
||||
} |
||||
|
||||
@Test |
||||
public void whenAccountWhitelistIsNotSetContainsShouldReturnTrue() { |
||||
when(permissioningConfig.isAccountWhitelistSet()).thenReturn(false); |
||||
controller = new AccountWhitelistController(permissioningConfig); |
||||
|
||||
assertThat(controller.contains("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void whenPermConfigHasAccountsShouldSetAccountsWhitelist() { |
||||
when(permissioningConfig.isAccountWhitelistSet()).thenReturn(true); |
||||
when(permissioningConfig.getAccountWhitelist()) |
||||
.thenReturn(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
controller = new AccountWhitelistController(permissioningConfig); |
||||
|
||||
assertThat(controller.isAccountWhiteListSet()).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void whenPermConfigHasAccountsShouldAddAllAccountsToWhitelist() { |
||||
when(permissioningConfig.isAccountWhitelistSet()).thenReturn(true); |
||||
when(permissioningConfig.getAccountWhitelist()) |
||||
.thenReturn(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
controller = new AccountWhitelistController(permissioningConfig); |
||||
|
||||
assertThat(controller.getAccountWhitelist()) |
||||
.contains("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); |
||||
} |
||||
|
||||
@Test |
||||
public void whenPermConfigContainsEmptyListOfAccountsContainsShouldReturnFalse() { |
||||
when(permissioningConfig.isAccountWhitelistSet()).thenReturn(true); |
||||
when(permissioningConfig.getAccountWhitelist()).thenReturn(new ArrayList<>()); |
||||
controller = new AccountWhitelistController(permissioningConfig); |
||||
|
||||
assertThat(controller.contains("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")).isFalse(); |
||||
} |
||||
|
||||
@Test |
||||
public void addAccountsWithInvalidAccountShouldReturnInvalidEntryResult() { |
||||
AddResult addResult = controller.addAccounts(Arrays.asList("0x0")); |
||||
|
||||
assertThat(addResult).isEqualTo(AddResult.ERROR_INVALID_ENTRY); |
||||
assertThat(controller.getAccountWhitelist()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void addDuplicatedAccountShouldReturnDuplicatedEntryResult() { |
||||
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
AddResult addResult = |
||||
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
|
||||
assertThat(addResult).isEqualTo(AddResult.ERROR_DUPLICATED_ENTRY); |
||||
assertThat(controller.getAccountWhitelist()) |
||||
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); |
||||
} |
||||
|
||||
@Test |
||||
public void addValidAccountsShouldReturnSuccessResult() { |
||||
AddResult addResult = |
||||
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
|
||||
assertThat(addResult).isEqualTo(AddResult.SUCCESS); |
||||
assertThat(controller.getAccountWhitelist()) |
||||
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"); |
||||
} |
||||
|
||||
@Test |
||||
public void removeExistingAccountShouldReturnSuccessResult() { |
||||
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
|
||||
RemoveResult removeResult = |
||||
controller.removeAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
|
||||
assertThat(removeResult).isEqualTo(RemoveResult.SUCCESS); |
||||
assertThat(controller.getAccountWhitelist()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void removeAbsentAccountShouldReturnAbsentEntryResult() { |
||||
RemoveResult removeResult = |
||||
controller.removeAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73")); |
||||
|
||||
assertThat(removeResult).isEqualTo(RemoveResult.ERROR_ABSENT_ENTRY); |
||||
assertThat(controller.getAccountWhitelist()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void removeInvalidAccountShouldReturnInvalidEntryResult() { |
||||
RemoveResult removeResult = controller.removeAccounts(Arrays.asList("0x0")); |
||||
|
||||
assertThat(removeResult).isEqualTo(RemoveResult.ERROR_INVALID_ENTRY); |
||||
assertThat(controller.getAccountWhitelist()).isEmpty(); |
||||
} |
||||
} |
Loading…
Reference in new issue