Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Increase coverage for .config
Browse files Browse the repository at this point in the history
- NodeFilterTest's acceptNullIpPatternAsCatchAllForNodes and doNotAcceptNullIpPatternAsCatchAllForInetAddresses seem to indicate a discrepancy between InetAddress and Node acceptance. Smells like a potential bug.
- NodeFilterTest's acceptInvalidNodeHostnameWhenUsingWildcard and doNotAcceptInvalidNodeHostnameWhenUsingPattern seem to indicate a discrepancy in acceptance logic. Smells like a potential bug.
- SystemPropertiesTest's testExposeBugWhereNonHexEncodedIsAcceptedWithoutValidation seems to indicate that private key validation is not correct.
  • Loading branch information
Erik committed Nov 2, 2017
1 parent f7f7d30 commit 09f7688
Show file tree
Hide file tree
Showing 8 changed files with 1,188 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) [2017] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/

package org.ethereum.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.read.ListAppender;
import org.ethereum.db.PruneManager;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Executors;

import static org.junit.Assert.*;

public class DefaultConfigTest {
/**
* TODO: For better testability, consider making setDefaultUncaughtExceptionHandler pluggable or Spring configurable as an autowired list
*/
@Test
public void testConstruction() throws InterruptedException {
ListAppender<ILoggingEvent> inMemoryAppender = new ListAppender<>();
inMemoryAppender.start();

Logger logger = (Logger) LoggerFactory.getLogger("general");
try {
logger.setLevel(Level.DEBUG);
logger.addAppender(inMemoryAppender);

// Registers the safety net
new DefaultConfig();

// Trigger an exception in the background
Executors.newSingleThreadExecutor().execute(new ExceptionThrower());
Thread.sleep(600);

ILoggingEvent firstException = inMemoryAppender.list.get(0);
assertEquals("Uncaught exception", firstException.getMessage());

IThrowableProxy cause = firstException.getThrowableProxy();
assertEquals("Unit test throwing an exception", cause.getMessage());
} finally {
inMemoryAppender.stop();
logger.detachAppender(inMemoryAppender);
}
}

@Test
public void testNoopPruneManager() throws Exception {
DefaultConfig defaultConfig = new DefaultConfig();
defaultConfig.config = new SystemProperties();
defaultConfig.config.overrideParams("database.prune.enabled", "false");

PruneManager noopPruneManager = defaultConfig.pruneManager();
// Should throw exception unless this is a NOOP prune manager
noopPruneManager.blockCommitted(null);
}

private static class ExceptionThrower implements Runnable {
@Override
public void run() {
throw new IllegalStateException("Unit test throwing an exception");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
package org.ethereum.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.google.common.io.Files;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
Expand All @@ -25,25 +29,26 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static org.ethereum.config.Initializer.DatabaseVersionHandler.Behavior;
import static org.ethereum.config.Initializer.DatabaseVersionHandler.Behavior.*;
import static org.junit.Assert.*;

/**
* Created by Stan Reshetnyk on 11.09.16.
*/
public class InitializerTest {

final Initializer.DatabaseVersionHandler resetHelper = new Initializer.DatabaseVersionHandler();
private final Initializer.DatabaseVersionHandler resetHelper = new Initializer.DatabaseVersionHandler();

File tempFile;
String databaseDir;
File versionFile;
private File tempFile;
private String databaseDir;
private File versionFile;

@Before
public void before() {
Expand Down Expand Up @@ -92,6 +97,7 @@ public void helper_shouldCreateVersionFile_whenOldVersion() {
// create database without version
SystemProperties props1 = withConfig(1, null);
resetHelper.process(props1);
//noinspection ResultOfMethodCallIgnored
versionFile.renameTo(new File(versionFile.getAbsoluteFile() + ".renamed"));

SystemProperties props2 = withConfig(2, IGNORE);
Expand All @@ -107,6 +113,7 @@ public void helper_shouldStop_whenNoVersionFileAndNotFirstVersion() throws IOExc
resetHelper.process(props);

// database is assumed to exist if dir is not empty
//noinspection ResultOfMethodCallIgnored
versionFile.renameTo(new File(versionFile.getAbsoluteFile() + ".renamed"));

resetHelper.process(props);
Expand Down Expand Up @@ -171,9 +178,42 @@ public void helper_shouldPutVersion_afterDatabaseReset() throws IOException {
assertFalse(testFile.exists()); // reset should have cleared file
}

@Test
public void helper_shouldPrintCapabilityEthVersion_whenInfoEnabled() {
SystemProperties system = new SystemProperties();
Initializer initializer = new Initializer();

ListAppender<ILoggingEvent> inMemoryAppender = new ListAppender<>();
inMemoryAppender.start();

Logger logger = (Logger) LoggerFactory.getLogger("general");
try {
logger.setLevel(Level.DEBUG);
logger.addAppender(inMemoryAppender);

initializer.postProcessBeforeInitialization(system, "initializerBean");
assertContainsLogLine(inMemoryAppender.list, "capability eth version: [62, 63]");
assertContainsLogLine(inMemoryAppender.list, "capability shh version: [3]");
assertContainsLogLine(inMemoryAppender.list, "capability bzz version: [0]");
} finally {
inMemoryAppender.stop();
logger.detachAppender(inMemoryAppender);
}
}

private void assertContainsLogLine(List<ILoggingEvent> lines, final String line) {
for (ILoggingEvent loggingEvent : lines) {
if (loggingEvent.getFormattedMessage().equals(line)) {
return;
}
}
fail("Could not find log line that matches: " + line);
}


// HELPERS

@SuppressWarnings("ResultOfMethodCallIgnored")
private File createFile() {
final File testFile = new File(databaseDir + "/empty.file");
testFile.getParentFile().mkdirs();
Expand Down Expand Up @@ -204,11 +244,11 @@ private SystemProperties withConfig(int databaseVersion, Behavior behavior) {

public static class SPO extends SystemProperties {

public SPO(Config config) {
SPO(Config config) {
super(config);
}

public void setDatabaseVersion(Integer databaseVersion) {
void setDatabaseVersion(Integer databaseVersion) {
this.databaseVersion = databaseVersion;
}
}
Expand Down
175 changes: 175 additions & 0 deletions ethereumj-core/src/test/java/org/ethereum/config/NodeFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c) [2017] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/

package org.ethereum.config;

import org.ethereum.net.rlpx.Node;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class NodeFilterTest {

private static final byte[] NODE_1 = "node-1".getBytes();
private static final byte[] NODE_2 = "node-2".getBytes();

@Test
public void addByHostIpPattern() throws Exception {
NodeFilter filter = new NodeFilter();

filter.add(NODE_1, "1.2.3.4");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
}

@Test
public void doNotAcceptDifferentNodeNameButSameIp() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "1.2.3.4");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertFalse(filter.accept(createTestNode("node-2", "1.2.3.4")));
}

@Test
public void acceptDifferentNodeWithoutNameButSameIp() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(null, "1.2.3.4");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.3.4")));
}

@Test
public void acceptDuplicateFilter() {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "1.2.3.4");
filter.add(NODE_1, "1.2.3.4");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
}

@Test
public void acceptDifferentNodeNameButOverlappingIp() {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "1.2.3.4");
filter.add(NODE_2, "1.2.3.*");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.3.99")));

assertFalse(filter.accept(createTestNode("invalid-1", "1.2.4.1")));
}

@Test
public void acceptMultipleWildcards() {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "1.2.3.*");
filter.add(NODE_2, "1.*.3.*");
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.3.99")));
assertTrue(filter.accept(createTestNode("node-2", "1.2.99.99")));
assertTrue(filter.accept(createTestNode("node-2", "1.99.99.99")));
assertFalse(filter.accept(createTestNode("node-2", "2.99.99.99")));
}

@Test
public void addInvalidNodeShouldThrowException() throws Exception {
NodeFilter filter = new NodeFilter();
assertFalse(filter.accept(createTestNode("invalid-1", null)));
}

@Test
public void neverAcceptOnEmptyFilter() throws Exception {
NodeFilter filter = new NodeFilter();
assertFalse(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertFalse(filter.accept(createTestNode("node-2", "1.2.3.4")));
assertFalse(filter.accept(createTestNode("node-2", "1.2.3.99")));
assertFalse(filter.accept(createTestNode("node-2", "1.2.99.99")));
assertFalse(filter.accept(createTestNode("node-2", "1.99.99.99")));
assertFalse(filter.accept(createTestNode("node-2", "2.99.99.99")));
}

@Test
public void acceptByInetAddress() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "8.*");
assertTrue(filter.accept(InetAddress.getByName("8.8.8.8")));
}

@Test
public void doNotAcceptTheCatchAllWildcard() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "*");
assertFalse(filter.accept(InetAddress.getByName("1.2.3.4")));
assertFalse(filter.accept(createTestNode("node-1", "255.255.255.255")));
}

@Test
public void acceptNullIpPatternAsCatchAllForNodes() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, null);
assertTrue(filter.accept(createTestNode("node-1", "1.2.3.4")));
assertTrue(filter.accept(createTestNode("node-1", "255.255.255.255")));
}

@Test
public void doNotAcceptNullIpPatternAsCatchAllForInetAddresses() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, null);
assertFalse(filter.accept(InetAddress.getByName("1.2.3.4")));
assertFalse(filter.accept(InetAddress.getByName("255.255.255.255")));
}

@Test
public void acceptLoopback() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(NODE_1, "127.0.0.1");
assertTrue(filter.accept(InetAddress.getByName("localhost")));
}

@Test
public void doNotAcceptInvalidNodeHostnameWhenUsingPattern() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(null, "1.2.3.4");

Node nodeWithInvalidHostname = new Node(
"enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
assertFalse(filter.accept(nodeWithInvalidHostname));
}

@Test
public void acceptInvalidNodeHostnameWhenUsingWildcard() throws Exception {
NodeFilter filter = new NodeFilter();
filter.add(null, null);

Node nodeWithInvalidHostname = new Node(
"enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
assertTrue(filter.accept(nodeWithInvalidHostname));
}

private static Node createTestNode(String nodeName, String hostIpPattern) {
return new Node("enode://" + Hex.toHexString(nodeName.getBytes()) + "@" + hostIpPattern + ":30303");
}

}
Loading

0 comments on commit 09f7688

Please sign in to comment.