查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 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.channel.unix.tests;
17  
18  import io.netty.channel.unix.DomainSocketAddress;
19  import io.netty.util.internal.PlatformDependent;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.lang.reflect.Method;
24  import java.net.InetAddress;
25  import java.net.InetSocketAddress;
26  import java.net.SocketAddress;
27  
28  import static org.junit.jupiter.api.Assumptions.assumeTrue;
29  
30  public final class UnixTestUtils {
31      private static final Object INET_LOOPBACK_UNAVAILABLE = new Object();
32      private static volatile Object inetLoopbackCache;
33  
34      /**
35       * @deprecated Use {@link #newDomainSocketAddress()} instead.
36       */
37      @Deprecated
38      public static DomainSocketAddress newSocketAddress() {
39          return newDomainSocketAddress();
40      }
41  
42      public static DomainSocketAddress newDomainSocketAddress() {
43          try {
44              File file;
45              do {
46                  file = PlatformDependent.createTempFile("NETTY", "UDS", null);
47                  if (!file.delete()) {
48                      throw new IOException("failed to delete: " + file);
49                  }
50              } while (file.getAbsolutePath().length() > 128);
51              return new DomainSocketAddress(file);
52          } catch (IOException e) {
53              throw new IllegalStateException(e);
54          }
55      }
56  
57      /**
58       * The JDK method may produce IPv4 loopback addresses where {@link io.netty.util.NetUtil#LOCALHOST} might be an
59       * IPv6 addresses.
60       * This difference can stress the system in different ways that are important to test.
61       */
62      public static SocketAddress newInetLoopbackSocketAddress() {
63          Object loopback = inetLoopbackCache;
64  
65          if (loopback == null) {
66              inetLoopbackCache = loopback = getLoopbackAddress();
67          }
68  
69          assumeTrue(loopback != INET_LOOPBACK_UNAVAILABLE, "InetAddress.getLoopbackAddress() is not available");
70          return new InetSocketAddress((InetAddress) loopback, 0);
71      }
72  
73      private static Object getLoopbackAddress() {
74          try {
75              Method method = InetAddress.class.getMethod("getLoopbackAddress");
76              return method.invoke(null);
77          } catch (Exception ignore) {
78              return INET_LOOPBACK_UNAVAILABLE;
79          }
80      }
81  
82      private UnixTestUtils() { }
83  }