From bf8983355e4ec0eaf4ef35b56761172b92360c12 Mon Sep 17 00:00:00 2001 From: Mark Terry Date: Tue, 5 Feb 2019 00:53:09 +1000 Subject: [PATCH] [NC-1968] Acceptance tests. --- .../perm/WhiteListContainsKeyAndValue.java | 44 ++++++++ .../tests/acceptance/dsl/jsonrpc/Perm.java | 6 ++ .../dsl/node/factory/PantheonNodeFactory.java | 20 ++++ .../WhitelistPersistorAcceptanceTest.java | 102 ++++++++++++++++++ .../AccountWhitelistController.java | 4 +- 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/perm/WhiteListContainsKeyAndValue.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/permissioning/WhitelistPersistorAcceptanceTest.java diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/perm/WhiteListContainsKeyAndValue.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/perm/WhiteListContainsKeyAndValue.java new file mode 100644 index 0000000000..9d7125fffc --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/perm/WhiteListContainsKeyAndValue.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; + +public class WhiteListContainsKeyAndValue implements Condition { + private final String val; + private final Path tempFile; + + public WhiteListContainsKeyAndValue(final String val, final Path tempFile) { + this.val = val; + this.tempFile = tempFile; + } + + @Override + public void verify(final Node node) { + Boolean result; + try (Stream lines = Files.lines(tempFile)) { + result = lines.anyMatch(line -> line.equals(val)); + } catch (IOException e) { + result = false; + } + assertThat(result).isTrue(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Perm.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Perm.java index e8bb0edd11..3dce3b4603 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Perm.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Perm.java @@ -19,8 +19,10 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.GetNodesWhitelistPopulated; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.RemoveAccountsFromWhitelistSuccessfully; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.RemoveNodeSuccess; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.WhiteListContainsKeyAndValue; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transactions; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; @@ -57,4 +59,8 @@ public Condition removeNodesFromWhitelist(final List enodeList) { public Condition getNodesWhitelist(final int expectedNodeNum) { return new GetNodesWhitelistPopulated(transactions.getNodesWhiteList(), expectedNodeNum); } + + public Condition expectPermissioningWhitelistFileKeyValue(final String val, final Path tempFile) { + return new WhiteListContainsKeyAndValue(val, tempFile); + } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java index ec7a819ea9..f83c15ad1f 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/factory/PantheonNodeFactory.java @@ -162,6 +162,26 @@ public PantheonNode createArchiveNodeWithRpcApis( .build()); } + public PantheonNode createNodeWithWhitelistsEnabled( + final String name, + final List nodesWhitelist, + final List accountsWhitelist, + final String tempFilePath) + throws IOException { + PermissioningConfiguration permissioningConfiguration = + PermissioningConfiguration.createDefault(); + permissioningConfiguration.setNodeWhitelist(nodesWhitelist); + permissioningConfiguration.setAccountWhitelist(accountsWhitelist); + permissioningConfiguration.setConfigurationFilePath(tempFilePath); + + return create( + new PantheonFactoryConfigurationBuilder() + .setName(name) + .setJsonRpcConfiguration(jsonRpcConfigWithPermissioning()) + .setPermissioningConfiguration(permissioningConfiguration) + .build()); + } + public PantheonNode createNodeWithNodesWhitelist( final String name, final List nodesWhitelist) throws IOException { PermissioningConfiguration permissioningConfiguration = diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/permissioning/WhitelistPersistorAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/permissioning/WhitelistPersistorAcceptanceTest.java new file mode 100644 index 0000000000..54b0634973 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/permissioning/WhitelistPersistorAcceptanceTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2019 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 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 java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; + +import org.assertj.core.util.Lists; +import org.junit.Before; +import org.junit.Test; + +public class WhitelistPersistorAcceptanceTest extends AcceptanceTestBase { + + private Node node; + private Account senderA; + private Account senderB; + private Path tempFile; + + private final String enode1 = + "enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; + private final String enode2 = + "enode://5f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; + private final String enode3 = + "enode://4f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.10:4567"; + + @Before + public void setUp() throws Exception { + senderA = accounts.getPrimaryBenefactor(); + senderB = accounts.getSecondaryBenefactor(); + tempFile = Files.createTempFile("test", "test"); + node = + pantheon.createNodeWithWhitelistsEnabled( + "node", + new ArrayList<>(), + Collections.singletonList(senderA.getAddress()), + tempFile.toAbsolutePath().toString()); + cluster.start(node); + } + + @Test + public void manipulatedAccountsWhitelistIsPersisted() { + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[\"%s\"]", "accounts-whitelist", senderA.getAddress()), tempFile)); + + node.execute(transactions.addAccountsToWhitelist(senderB.getAddress())); + node.verify(perm.expectAccountsWhitelist(senderA.getAddress(), senderB.getAddress())); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format( + "%s=[\"%s\",\"%s\"]", + "accounts-whitelist", senderA.getAddress(), senderB.getAddress()), + tempFile)); + + node.execute(transactions.removeAccountsFromWhitelist(senderB.getAddress())); + node.verify(perm.expectAccountsWhitelist(senderA.getAddress())); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[\"%s\"]", "accounts-whitelist", senderA.getAddress()), tempFile)); + + node.execute(transactions.removeAccountsFromWhitelist(senderA.getAddress())); + node.verify(perm.expectAccountsWhitelist()); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[]", "accounts-whitelist"), tempFile)); + } + + @Test + public void manipulatedNodesWhitelistIsPersisted() { + node.verify(perm.addNodesToWhitelist(Lists.newArrayList(enode1, enode2))); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[\"%s\",\"%s\"]", "nodes-whitelist", enode1, enode2), tempFile)); + + node.verify(perm.removeNodesFromWhitelist(Lists.newArrayList(enode1))); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[\"%s\"]", "nodes-whitelist", enode2), tempFile)); + + node.verify(perm.addNodesToWhitelist(Lists.newArrayList(enode1, enode3))); + node.verify( + perm.expectPermissioningWhitelistFileKeyValue( + String.format("%s=[\"%s\",\"%s\",\"%s\"]", "nodes-whitelist", enode2, enode1, enode3), + tempFile)); + } +} diff --git a/ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistController.java b/ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistController.java index d24a641c0c..06004b015c 100644 --- a/ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistController.java +++ b/ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistController.java @@ -53,7 +53,7 @@ public WhitelistOperationResult addAccounts(final List accounts) { final List oldWhitelist = new ArrayList<>(this.accountWhitelist); this.accountWhitelist.addAll(accounts); try { - updateConfigurationFile(accounts); + updateConfigurationFile(accountWhitelist); } catch (IOException e) { revertState(oldWhitelist); return WhitelistOperationResult.ERROR_WHITELIST_PERSIST_FAIL; @@ -75,7 +75,7 @@ public WhitelistOperationResult removeAccounts(final List accounts) { this.accountWhitelist.removeAll(accounts); try { - updateConfigurationFile(accounts); + updateConfigurationFile(accountWhitelist); } catch (IOException e) { revertState(oldWhitelist); return WhitelistOperationResult.ERROR_WHITELIST_PERSIST_FAIL;