Skip to content

Commit 6e17db2

Browse files
authored
Do not decode plain hostname (#337)
1 parent c706c6a commit 6e17db2

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/main/java/de/gesellix/docker/client/filesocket/FileSocket.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public String encodeHostname(String hostname) {
1313

1414
public String decodeHostname(InetAddress address) {
1515
String hostName = address.getHostName();
16+
if (!hostName.endsWith(SOCKET_MARKER)) {
17+
return hostName;
18+
}
1619
return new HostnameEncoder().decode(hostName.substring(0, hostName.indexOf(SOCKET_MARKER)));
1720
}
1821
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package de.gesellix.docker.client.filesocket;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.net.InetAddress;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class FileSocketTest {
10+
11+
@Test
12+
public void shouldEncodeHostname() throws Exception {
13+
final String plainHostname = "for.test";
14+
try (TestSocket testSocket = new TestSocket()) {
15+
String encodedHostname = testSocket.encodeHostname(plainHostname);
16+
assertEquals("666f722e74657374.socket", encodedHostname);
17+
}
18+
}
19+
20+
@Test
21+
public void shouldDecodeEncodedHostname() throws Exception {
22+
final String encodedHostname = "666f722e74657374.socket";
23+
try (TestSocket testSocket = new TestSocket()) {
24+
String decodedHostname = testSocket.decodeHostname(InetAddress.getByAddress(encodedHostname, new byte[]{0, 0, 0, 1}));
25+
assertEquals("for.test", decodedHostname);
26+
}
27+
}
28+
29+
@Test
30+
public void shouldNotDecodePlainHostname() throws Exception {
31+
final String plainHostname = "for.test";
32+
try (TestSocket testSocket = new TestSocket()) {
33+
String decodedHostname = testSocket.decodeHostname(InetAddress.getByAddress(plainHostname, new byte[]{0, 0, 0, 1}));
34+
assertEquals(plainHostname, decodedHostname);
35+
}
36+
}
37+
38+
static class TestSocket extends FileSocket {
39+
}
40+
41+
}

0 commit comments

Comments
 (0)