Skip to content

Commit 2f21f50

Browse files
committed
Fix PostgresUtils.isLocalhost()
This fixes a regression that was introduced with 6f96ff4
1 parent c939d40 commit 2f21f50

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/main/java/de/cronn/postgres/snapshot/util/PostgresUtils.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,21 @@ private static String prepareHostname(String host) {
142142
}
143143
}
144144

145-
private static boolean isLocalhost(String host) {
145+
static boolean isLocalhost(String host) {
146+
if (host.equals("localhost")) {
147+
return true;
148+
}
149+
InetAddress inetAddress;
146150
try {
147-
InetAddress localHost = InetAddress.getLocalHost();
148-
return localHost.getHostName().equals(host) || localHost.getHostAddress().equals(host);
151+
inetAddress = InetAddress.getByName(host);
149152
} catch (UnknownHostException e) {
150-
throw new RuntimeException("Failed resolve localhost", e);
153+
log.trace("Failed to resolve host '{}'", host, e);
154+
return false;
155+
}
156+
try {
157+
return InetAddress.getByName("localhost").equals(inetAddress);
158+
} catch (UnknownHostException e) {
159+
throw new RuntimeException(e);
151160
}
152161
}
153162

src/test/java/de/cronn/postgres/snapshot/util/PostgresUtilsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import static org.assertj.core.api.Assertions.*;
44

5+
import java.net.InetAddress;
56
import java.net.URISyntaxException;
67

78
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.ValueSource;
811

912
class PostgresUtilsTest {
1013

@@ -23,6 +26,13 @@ void testParseConnectionInformation_unknownHost() {
2326
.withMessage("org.postgresql.util.PSQLException: The connection attempt failed.");
2427
}
2528

29+
@Test
30+
void testParseConnectionInformation_failsToConnect() {
31+
assertThatExceptionOfType(RuntimeException.class)
32+
.isThrownBy(() -> PostgresUtils.parseConnectionInformation("jdbc:postgresql://10.0.0.1/test?connectTimeout=1", "user", "password"))
33+
.withMessage("org.postgresql.util.PSQLException: The connection attempt failed.");
34+
}
35+
2636
@Test
2737
void testParseConnectionInformation_driverFailsToParseJdbcUrl() {
2838
assertThatExceptionOfType(RuntimeException.class)
@@ -37,4 +47,18 @@ void testParseConnectionInformation_failToConnectToPostgresDatabase() {
3747
.withMessage("org.postgresql.util.PSQLException: Connection to localhost:5432 refused." +
3848
" Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.");
3949
}
50+
51+
@ParameterizedTest
52+
@ValueSource(strings = { "localhost", "127.0.0.1" })
53+
void testIsLocalHost_positive(String host) {
54+
assertThat(PostgresUtils.isLocalhost(host)).isTrue();
55+
}
56+
57+
@Test
58+
void testIsLocalHost_negative() throws Exception {
59+
assertThat(PostgresUtils.isLocalhost("some.host.local")).isFalse();
60+
assertThat(PostgresUtils.isLocalhost("1.2.3.4")).isFalse();
61+
InetAddress localHost = InetAddress.getLocalHost();
62+
assertThat(PostgresUtils.isLocalhost(localHost.getHostName())).isFalse();
63+
}
4064
}

0 commit comments

Comments
 (0)