Skip to content

Commit

Permalink
Merge pull request #7309 from alvasw/BtcNodes_Fix_hostname_parsing
Browse files Browse the repository at this point in the history
BtcNodes: Fix hostname parsing
  • Loading branch information
alejandrogarcia83 authored Nov 20, 2024
2 parents 633c9c1 + 41fbb75 commit 9aed52f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/bisq/core/btc/nodes/BtcNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -125,6 +126,10 @@ public static BtcNode fromFullAddress(String fullAddress) {
host = parts[0];
if (parts.length == 2)
port = Integer.parseInt(parts[1]);

if (isHostName(host)) {
return new BtcNode(host, null, null, port, null);
}
}

checkArgument(host.length() > 0, "BtcNode address format not recognised");
Expand Down Expand Up @@ -172,5 +177,11 @@ public String getId() {
String onionAddress = this.onionAddress == null ? "" : this.onionAddress;
return operator + ": [" + address + onionAddress + "]";
}

private static boolean isHostName(String hostName) {
String ipV4RegEx = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
boolean isIpV4Address = Pattern.matches(ipV4RegEx, hostName);
return !isIpV4Address && !hostName.endsWith(".onion");
}
}
}
67 changes: 67 additions & 0 deletions core/src/test/java/bisq/core/btc/nodes/BtcNodeTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bisq.core.btc.nodes;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -18,4 +20,69 @@ void hardcodedAndFilterProvidedNodeShouldBeEqual() {

assertThat(aliceHardcodedBtcNode, equalTo(aliceNodeFromFilter));
}

@ParameterizedTest
@ValueSource(strings = {"123.456.890.123", "2001:db8:85a3:8d3:1319:8a2e:370"})
void fromFullAddressIpNoPort(String address) {
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(address);
assertThat(btcNode.getAddress(), equalTo(address));
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
}

@Test
void fromFullAddressIpV4() {
String address = "123.456.890.123";
int port = 4567;
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(address + ":" + port);

assertThat(btcNode.getAddress(), equalTo(address));
assertThat(btcNode.getPort(), equalTo(port));
}

@Test
void fromFullAddressIpV6() {
String address = "2001:db8:85a3:8d3:1319:8a2e:370";
int port = 7348;
String fullAddress = "[" + address + "]:" + port;
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(fullAddress);

assertThat(btcNode.getAddress(), equalTo(address));
assertThat(btcNode.getPort(), equalTo(port));
}

@Test
void fromFullAddressHostNameNoPort() {
String hostname = "btc-node.bisq.network";
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(hostname);
assertThat(btcNode.getHostName(), equalTo(hostname));
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
}

@Test
void fromFullAddressHostName() {
String hostname = "btc-node.bisq.network";
int port = 4567;
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(hostname + ":" + port);

assertThat(btcNode.getHostName(), equalTo(hostname));
assertThat(btcNode.getPort(), equalTo(port));
}

@Test
void fromFullAddressOnionNoPort() {
String onionAddress = "alice.onion";
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(onionAddress);
assertThat(btcNode.getOnionAddress(), equalTo(onionAddress));
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
}

@Test
void fromFullAddressOnion() {
String onionAddress = "alice.onion";
int port = 4567;
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(onionAddress + ":" + port);

assertThat(btcNode.getOnionAddress(), equalTo(onionAddress));
assertThat(btcNode.getPort(), equalTo(port));
}
}

0 comments on commit 9aed52f

Please sign in to comment.