Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[NC-1968] Acceptance tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-terry committed Feb 4, 2019
1 parent b37b408 commit bf89833
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> lines = Files.lines(tempFile)) {
result = lines.anyMatch(line -> line.equals(val));
} catch (IOException e) {
result = false;
}
assertThat(result).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,4 +59,8 @@ public Condition removeNodesFromWhitelist(final List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ public PantheonNode createArchiveNodeWithRpcApis(
.build());
}

public PantheonNode createNodeWithWhitelistsEnabled(
final String name,
final List<URI> nodesWhitelist,
final List<String> 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<URI> nodesWhitelist) throws IOException {
PermissioningConfiguration permissioningConfiguration =
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WhitelistOperationResult addAccounts(final List<String> accounts) {
final List<String> 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;
Expand All @@ -75,7 +75,7 @@ public WhitelistOperationResult removeAccounts(final List<String> accounts) {

this.accountWhitelist.removeAll(accounts);
try {
updateConfigurationFile(accounts);
updateConfigurationFile(accountWhitelist);
} catch (IOException e) {
revertState(oldWhitelist);
return WhitelistOperationResult.ERROR_WHITELIST_PERSIST_FAIL;
Expand Down

0 comments on commit bf89833

Please sign in to comment.