This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #946 from weisheme/increase_test_coverage_for_config
Increase test coverage for config
- Loading branch information
Showing
11 changed files
with
1,465 additions
and
21 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
ethereumj-core/src/test/java/org/ethereum/config/DefaultConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
ethereumj-core/src/test/java/org/ethereum/config/NodeFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
Oops, something went wrong.