1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.testsuite.transport.socket;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.bootstrap.ServerBootstrap;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.channel.ChannelInboundHandlerAdapter;
23 import io.netty.util.concurrent.ImmediateEventExecutor;
24 import io.netty.util.concurrent.Promise;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.TestInfo;
27 import org.junit.jupiter.api.Timeout;
28
29 import java.net.SocketAddress;
30 import java.util.concurrent.TimeUnit;
31
32 import static org.junit.jupiter.api.Assertions.assertNull;
33
34 public abstract class SocketAddressesTest extends AbstractSocketTest {
35
36 @Test
37 @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
38 public void testAddresses(TestInfo testInfo) throws Throwable {
39 run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
40 @Override
41 public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
42 testAddresses(serverBootstrap, bootstrap, true);
43 }
44 });
45 }
46
47 @Test
48 @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
49 public void testAddressesConnectWithoutLocalAddress(TestInfo testInfo) throws Throwable {
50 run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
51 @Override
52 public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
53 testAddresses(serverBootstrap, bootstrap, false);
54 }
55 });
56 }
57
58 protected abstract void assertAddress(SocketAddress address);
59
60 private void testAddresses(ServerBootstrap sb, Bootstrap cb, boolean withLocalAddress) throws Throwable {
61 Channel serverChannel = null;
62 Channel clientChannel = null;
63 try {
64 final Promise<SocketAddress> localAddressPromise = ImmediateEventExecutor.INSTANCE.newPromise();
65 final Promise<SocketAddress> remoteAddressPromise = ImmediateEventExecutor.INSTANCE.newPromise();
66 serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() {
67 @Override
68 public void channelActive(ChannelHandlerContext ctx) {
69 localAddressPromise.setSuccess(ctx.channel().localAddress());
70 remoteAddressPromise.setSuccess(ctx.channel().remoteAddress());
71 }
72 }).bind().syncUninterruptibly().channel();
73
74 clientChannel = cb.handler(new ChannelInboundHandlerAdapter()).register().syncUninterruptibly().channel();
75
76 assertNull(clientChannel.localAddress());
77 assertNull(clientChannel.remoteAddress());
78
79 if (withLocalAddress) {
80 clientChannel.connect(serverChannel.localAddress(), newSocketAddress()).syncUninterruptibly().channel();
81 } else {
82 clientChannel.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
83 }
84
85 assertAddress(clientChannel.localAddress());
86 assertAddress(clientChannel.remoteAddress());
87
88 assertAddress(localAddressPromise.get());
89 assertAddress(remoteAddressPromise.get());
90 } finally {
91 if (clientChannel != null) {
92 clientChannel.close().syncUninterruptibly();
93 }
94 if (serverChannel != null) {
95 serverChannel.close().syncUninterruptibly();
96 }
97 }
98 }
99 }