Exemplo n.º 1
0
 static {
   boolean disabled;
   if (SystemPropertyUtil.get("io.netty.noResourceLeakDetection") != null) {
     boolean disabled = SystemPropertyUtil.getBoolean("io.netty.noResourceLeakDetection", false);
     logger.debug("-Dio.netty.noResourceLeakDetection: {}", Boolean.valueOf(disabled));
     logger.warn(
         "-Dio.netty.noResourceLeakDetection is deprecated. Use '-D{}={}' instead.",
         "io.netty.leakDetectionLevel",
         DEFAULT_LEVEL.name().toLowerCase());
   } else {
     disabled = false;
   }
   Level defaultLevel = disabled ? Level.DISABLED : DEFAULT_LEVEL;
   String levelStr =
       SystemPropertyUtil.get("io.netty.leakDetectionLevel", defaultLevel.name())
           .trim()
           .toUpperCase();
   Level level = DEFAULT_LEVEL;
   for (Level l : EnumSet.allOf(Level.class)) {
     if ((levelStr.equals(l.name())) || (levelStr.equals(String.valueOf(l.ordinal())))) {
       level = l;
     }
   }
   level = level;
   if (logger.isDebugEnabled()) {
     logger.debug("-D{}: {}", "io.netty.leakDetectionLevel", level.name().toLowerCase());
   }
 }
  private static String sanitizeUri(String rootPath, String uri) {
    // Decode the path.
    try {
      uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new Error(e);
    }

    if (uri.isEmpty() || uri.charAt(0) != '/') {
      return null;
    }

    // Convert file separators.
    uri = uri.replace('/', File.separatorChar);

    // Simplistic dumb security check.
    // You will have to do something serious in the production environment.
    if (uri.contains(File.separator + '.')
        || uri.contains('.' + File.separator)
        || uri.charAt(0) == '.'
        || uri.charAt(uri.length() - 1) == '.'
        || INSECURE_URI.matcher(uri).matches()) {
      return null;
    }

    // Convert to absolute path.
    return SystemPropertyUtil.get("user.dir") + File.separator + rootPath + File.separator + uri;
  }
  static {
    DEFAULT_EVENT_LOOP_THREADS =
        Math.max(
            1,
            SystemPropertyUtil.getInt(
                "io.netty.eventLoopThreads", Runtime.getRuntime().availableProcessors() * 2));

    if (logger.isDebugEnabled()) {
      logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
    }
  }
public class SocketConnectionAttemptTest extends AbstractClientSocketTest {

  private static final String BAD_HOST =
      SystemPropertyUtil.get("io.netty.testsuite.badHost", "netty.io");
  private static final int BAD_PORT =
      SystemPropertyUtil.getInt("io.netty.testsuite.badPort", 65535);

  static {
    InternalLogger logger = InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class);
    logger.debug("-Dio.netty.testsuite.badHost: {}", BAD_HOST);
    logger.debug("-Dio.netty.testsuite.badPort: {}", BAD_PORT);
  }

  @Test(timeout = 30000)
  public void testConnectTimeout() throws Throwable {
    run();
  }

  public void testConnectTimeout(Bootstrap cb) throws Throwable {
    cb.handler(new TestHandler()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
    ChannelFuture future = cb.connect(BAD_HOST, BAD_PORT);
    try {
      assertThat(future.await(3000), is(true));
    } finally {
      future.channel().close();
    }
  }

  @Test
  public void testConnectCancellation() throws Throwable {
    // Check if the test can be executed or should be skipped because of no network/internet
    // connection
    // See https://github.com/netty/netty/issues/1474
    boolean badHostTimedOut = true;
    Socket socket = new Socket();
    try {
      socket.connect(new InetSocketAddress(BAD_HOST, BAD_PORT), 10);
    } catch (ConnectException e) {
      badHostTimedOut = false;
      // is thrown for no route to host when using Socket connect
    } catch (Exception e) {
      // ignore
    } finally {
      try {
        socket.close();
      } catch (IOException e) {
        // ignore
      }
    }

    assumeThat(
        "The connection attempt to " + BAD_HOST + " does not time out.", badHostTimedOut, is(true));

    run();
  }

  public void testConnectCancellation(Bootstrap cb) throws Throwable {
    cb.handler(new TestHandler()).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
    ChannelFuture future = cb.connect(BAD_HOST, BAD_PORT);
    try {
      if (future.await(1000)) {
        if (future.isSuccess()) {
          fail("A connection attempt to " + BAD_HOST + " must not succeed.");
        } else {
          throw future.cause();
        }
      }

      if (future.cancel(true)) {
        assertThat(future.channel().closeFuture().await(500), is(true));
        assertThat(future.isCancelled(), is(true));
      } else {
        // Cancellation not supported by the transport.
      }
    } finally {
      future.channel().close();
    }
  }

  private static class TestHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class)
          .warn("Unexpected exception:", cause);
    }
  }
}
Exemplo n.º 5
0
 private void linuxOnly() {
   String osName = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
   assumeTrue("Only supported on Linux, your os is " + osName, osName.startsWith("linux"));
 }