Skip to content

Commit

Permalink
Merge pull request #967 from weisheme/issue_946
Browse files Browse the repository at this point in the history
#946 - Fixes for small configuration bugs found by tests
  • Loading branch information
mkalinin committed Jun 14, 2018
2 parents 6488ed5 + 29626ef commit c887e11
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ public Entry(byte[] nodeId, String hostIpPattern) {
}

public boolean accept(InetAddress nodeAddr) {
if (hostIpPattern == null) return true;
String ip = nodeAddr.getHostAddress();
return hostIpPattern != null && ip.startsWith(hostIpPattern);
}

public boolean accept(Node node) {
try {
return (nodeId == null || Arrays.equals(node.getId(), nodeId))
&& (hostIpPattern == null || accept(InetAddress.getByName(node.getHost())));
boolean shouldAcceptNodeId = nodeId == null || Arrays.equals(node.getId(), nodeId);
if (!shouldAcceptNodeId) {
return false;
}
InetAddress nodeAddress = InetAddress.getByName(node.getHost());
return (hostIpPattern == null || accept(nodeAddress));
} catch (UnknownHostException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.ethereum.net.rlpx.Node;
import org.ethereum.util.BuildInfo;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.Utils;
import org.ethereum.validator.BlockCustomHashRule;
import org.ethereum.validator.BlockHeaderValidator;
import org.slf4j.Logger;
Expand Down Expand Up @@ -665,7 +666,7 @@ public String customSolcPath() {
public String privateKey() {
if (config.hasPath("peer.privateKey")) {
String key = config.getString("peer.privateKey");
if (key.length() != 64) {
if (key.length() != 64 || !Utils.isHexEncoded(key)) {
throw new RuntimeException("The peer.privateKey needs to be Hex encoded and 32 byte length");
}
return key;
Expand Down
13 changes: 13 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,17 @@ public static void sleep(long ms) {
Thread.currentThread().interrupt();
}
}

public static boolean isHexEncoded(String value) {
if (value == null) return false;
if ("".equals(value)) return true;

try {
//noinspection ResultOfMethodCallIgnored
new BigInteger(value, 16);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
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 {

Expand Down Expand Up @@ -134,11 +132,11 @@ public void acceptNullIpPatternAsCatchAllForNodes() throws Exception {
}

@Test
public void doNotAcceptNullIpPatternAsCatchAllForInetAddresses() throws Exception {
public void acceptNullIpPatternAsCatchAllForInetAddresses() 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")));
assertTrue(filter.accept(InetAddress.getByName("1.2.3.4")));
assertTrue(filter.accept(InetAddress.getByName("255.255.255.255")));
}

@Test
Expand All @@ -153,19 +151,17 @@ 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");
Node nodeWithInvalidHostname = new Node("enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
assertFalse(filter.accept(nodeWithInvalidHostname));
}

@Test
public void acceptInvalidNodeHostnameWhenUsingWildcard() throws Exception {
public void doNotAcceptInvalidNodeHostnameWhenUsingWildcard() 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));
Node nodeWithInvalidHostname = new Node("enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
assertFalse(filter.accept(nodeWithInvalidHostname));
}

private static Node createTestNode(String nodeName, String hostIpPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ private void assertInvalidPrivateKey(byte[] privateKey) {
} catch (RuntimeException ignore) { }
}

@Ignore
@Test
public void testExposeBugWhereNonHexEncodedIsAcceptedWithoutValidation() {
SystemProperties props = new SystemProperties();
Expand Down
21 changes: 20 additions & 1 deletion ethereumj-core/src/test/java/org/ethereum/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.math.BigInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Roman Mandeleil
Expand Down Expand Up @@ -105,9 +107,26 @@ public void testAddressStringToBytes() {
assertEquals(expected, result);
}

@Test
public void testIsHexEncoded() {
assertTrue(Utils.isHexEncoded("AAA"));
assertTrue(Utils.isHexEncoded("6c386a4b26f73c802f34673f7248bb118f97424a"));
assertFalse(Utils.isHexEncoded(null));
assertFalse(Utils.isHexEncoded("I am not hex"));
assertTrue(Utils.isHexEncoded(""));
assertTrue(Utils.isHexEncoded(
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a"));
}

@Test
public void testLongToTimePeriod() {
assertEquals("2.99s", Utils.longToTimePeriod(3000 - 12));
assertEquals("1d21h", Utils.longToTimePeriod(45L * 3600 * 1000));
}
}
}

0 comments on commit c887e11

Please sign in to comment.