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

[FIX] Changed Enode length error String implementation. #1486

Merged
merged 1 commit into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.p2p.P2pDisabledException;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.util.enode.EnodeURL;

public abstract class AdminModifyPeer implements JsonRpcMethod {

Expand All @@ -43,7 +42,9 @@ public JsonRpcResponse response(final JsonRpcRequest req) {
} catch (final InvalidJsonRpcParameters e) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS);
} catch (final IllegalArgumentException e) {
if (e.getMessage().contains(EnodeURL.INVALID_NODE_ID_LENGTH)) {
if (e.getMessage()
.endsWith(
"Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.")) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.ENODE_ID_INVALID);
} else {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.PARSE_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.response;

import tech.pegasys.pantheon.util.enode.EnodeURL;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down Expand Up @@ -101,7 +99,9 @@ public enum JsonRpcError {
CANT_CONNECT_TO_LOCAL_PEER(-32100, "Cannot add local node as peer."),

// Invalid input errors
ENODE_ID_INVALID(-32000, EnodeURL.INVALID_NODE_ID_LENGTH);
ENODE_ID_INVALID(
-32000,
"Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.");

private final int code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ public void requestHasInvalidEnode() {
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void requestHasInvalidEnodeLength() {
String invalidLengthEnode =
"enode://"
+ "0000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303";
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_addPeer", new String[] {invalidLengthEnode});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.ENODE_ID_INVALID);

final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void requestAddsValidEnode() {
when(p2pNetwork.addMaintainConnectionPeer(any())).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public class EnodeURL {
private final OptionalInt listeningPort;
private final OptionalInt discoveryPort;

// Error messages
public static String INVALID_NODE_ID_LENGTH =
"Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.";

private EnodeURL(
final BytesValue nodeId,
final InetAddress address,
Expand Down Expand Up @@ -91,7 +87,9 @@ public static EnodeURL fromURI(final URI uri) {

checkArgument(
uri.getScheme().equalsIgnoreCase("enode"), "Invalid URI scheme (must equal \"enode\").");
checkArgument(NODE_ID_PATTERN.matcher(uri.getUserInfo()).matches(), INVALID_NODE_ID_LENGTH);
checkArgument(
NODE_ID_PATTERN.matcher(uri.getUserInfo()).matches(),
"Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.");

final BytesValue id = BytesValue.fromHexString(uri.getUserInfo());
String host = uri.getHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ public void fromStringInvalidNodeIdLengthHasDescriptiveMessage() {
String.format("enode://%s@%s:%d", VALID_NODE_ID.substring(1), IPV4_ADDRESS, P2P_PORT);
assertThatThrownBy(() -> EnodeURL.fromString(invalidEnodeURL))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("node ID must have exactly 128 hexadecimal");
.hasMessageEndingWith(
"Invalid node ID: node ID must have exactly 128 hexadecimal characters and should not include any '0x' hex prefix.");
}

@Test
Expand Down