From 41467e3112aa908f286eeb21e9630f3f5a9500fd Mon Sep 17 00:00:00 2001 From: Rajat Gupta Date: Fri, 4 Apr 2025 14:47:56 +0530 Subject: [PATCH 1/2] Add some additional tests for SocketChannelInterceptor Signed-off-by: Rajat Gupta --- .../javaagent/SocketChannelInterceptorTests.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java b/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java index 38b21f79cf0f6..c8af096faf6cf 100644 --- a/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java +++ b/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.net.InetSocketAddress; +import java.net.InetAddress; import java.net.UnixDomainSocketAddress; import java.nio.channels.SocketChannel; @@ -19,11 +20,24 @@ public class SocketChannelInterceptorTests extends AgentTestCase { @Test - public void test() throws IOException { + public void testConnections() throws IOException { try (SocketChannel channel = SocketChannel.open()) { assertThrows(SecurityException.class, () -> channel.connect(new InetSocketAddress("localhost", 9200))); assertThrows(SecurityException.class, () -> channel.connect(UnixDomainSocketAddress.of("fake-path"))); + + assertThrows(SecurityException.class, () -> channel.connect(new InetSocketAddress("opensearch.org", 80))); + } + } + + @Test + public void testHostnameResolution() throws IOException { + try (SocketChannel channel = SocketChannel.open()) { + InetAddress[] addresses = InetAddress.getAllByName("localhost"); + for (InetAddress address : addresses) { + assertThrows(SecurityException.class, + () -> channel.connect(new InetSocketAddress(address, 9200))); + } } } } From cd012b9cdaabf2348e2078b706cdb3b022d5c2c2 Mon Sep 17 00:00:00 2001 From: Rajat Gupta Date: Fri, 4 Apr 2025 14:52:45 +0530 Subject: [PATCH 2/2] Fix format violations Signed-off-by: Rajat Gupta --- .../opensearch/javaagent/SocketChannelInterceptorTests.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java b/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java index c8af096faf6cf..3a4a7b5576ebb 100644 --- a/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java +++ b/libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java @@ -11,8 +11,8 @@ import org.junit.Test; import java.io.IOException; -import java.net.InetSocketAddress; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.UnixDomainSocketAddress; import java.nio.channels.SocketChannel; @@ -35,8 +35,7 @@ public void testHostnameResolution() throws IOException { try (SocketChannel channel = SocketChannel.open()) { InetAddress[] addresses = InetAddress.getAllByName("localhost"); for (InetAddress address : addresses) { - assertThrows(SecurityException.class, - () -> channel.connect(new InetSocketAddress(address, 9200))); + assertThrows(SecurityException.class, () -> channel.connect(new InetSocketAddress(address, 9200))); } } }