View Javadoc
1   /*
2    * Copyright 2023 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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  }