use of io.netty.handler.codec.string.StringDecoder in project netty by netty.
the class SocketStringEchoTest method testStringEcho.
private static void testStringEcho(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
sb.childOption(ChannelOption.AUTO_READ, autoRead);
cb.option(ChannelOption.AUTO_READ, autoRead);
Promise<Void> serverDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
Promise<Void> clientDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
final StringEchoHandler sh = new StringEchoHandler(autoRead, serverDonePromise);
final StringEchoHandler ch = new StringEchoHandler(autoRead, clientDonePromise);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel sch) throws Exception {
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addAfter("decoder", "handler", sh);
}
});
cb.handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel sch) throws Exception {
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
sch.pipeline().addAfter("decoder", "handler", ch);
}
});
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect(sc.localAddress()).sync().channel();
for (String element : data) {
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
cc.writeAndFlush(element + delimiter);
}
ch.donePromise.sync();
sh.donePromise.sync();
sh.channel.close().sync();
ch.channel.close().sync();
sc.close().sync();
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
throw sh.exception.get();
}
if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
throw ch.exception.get();
}
if (sh.exception.get() != null) {
throw sh.exception.get();
}
if (ch.exception.get() != null) {
throw ch.exception.get();
}
}
use of io.netty.handler.codec.string.StringDecoder in project flink by apache.
the class NettyClientServerSslTest method testSslHandshakeError.
/**
* Verify SSL handshake error when untrusted server certificate is used.
*/
@Test
public void testSslHandshakeError() throws Exception {
NettyProtocol protocol = new NoOpProtocol();
Configuration config = createSslConfig();
// Use a server certificate which is not present in the truststore
config.setString(SecurityOptions.SSL_INTERNAL_KEYSTORE, "src/test/resources/untrusted.keystore");
NettyTestUtil.NettyServerAndClient serverAndClient;
try (NetUtils.Port port = NetUtils.getAvailablePort()) {
NettyConfig nettyConfig = createNettyConfig(config, port);
serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
}
Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
Channel ch = NettyTestUtil.connect(serverAndClient);
ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
// Attempting to write data over ssl should fail
assertFalse(ch.writeAndFlush("test").await().isSuccess());
NettyTestUtil.shutdown(serverAndClient);
}
use of io.netty.handler.codec.string.StringDecoder in project flink by apache.
the class NettyClientServerSslTest method testClientUntrustedCertificate.
@Test
public void testClientUntrustedCertificate() throws Exception {
final Configuration serverConfig = createSslConfig();
final Configuration clientConfig = createSslConfig();
// give the client a different keystore / certificate
clientConfig.setString(SecurityOptions.SSL_INTERNAL_KEYSTORE, "src/test/resources/untrusted.keystore");
NettyServerAndClient serverAndClient;
try (NetUtils.Port serverPort = NetUtils.getAvailablePort();
NetUtils.Port clientPort = NetUtils.getAvailablePort()) {
final NettyConfig nettyServerConfig = createNettyConfig(serverConfig, serverPort);
final NettyConfig nettyClientConfig = createNettyConfig(clientConfig, clientPort);
final NettyBufferPool bufferPool = new NettyBufferPool(1);
final NettyProtocol protocol = new NoOpProtocol();
final NettyServer server = NettyTestUtil.initServer(nettyServerConfig, protocol, bufferPool);
final NettyClient client = NettyTestUtil.initClient(nettyClientConfig, protocol, bufferPool);
serverAndClient = new NettyServerAndClient(server, client);
}
Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
final Channel ch = NettyTestUtil.connect(serverAndClient);
ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
// Attempting to write data over ssl should fail
assertFalse(ch.writeAndFlush("test").await().isSuccess());
NettyTestUtil.shutdown(serverAndClient);
}
Aggregations