Repository navigation
Expand file tree
/
Copy pathNativeTlsChannelTest.java
More file actions
101 lines (96 loc) 路 4.15 KB
/
Copy pathNativeTlsChannelTest.java
File metadata and controls
101 lines (96 loc) 路 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import dev.elide.bemo.transport.*;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public final class NativeTlsChannelTest {
public static void main(String[] args) throws Exception {
TransportNative api = new BackendTransport(new FfmTransportNative(Path.of(args[0])));
verify(
api,
java.nio.file.Files.readAllBytes(Path.of(args[1])),
java.nio.file.Files.readAllBytes(Path.of(args[2])));
}
public static void verify(TransportNative api, byte[] cert, byte[] key) throws Exception {
NativeTlsContext serverTls = NativeTlsContext.server(api, cert, key, "h2");
NativeTlsContext clientTls = NativeTlsContext.client(api, cert, "h2");
EventLoopGroup group =
new MultiThreadIoEventLoopGroup(
2, NativeIoHandler.newFactory(api, 0, 128, 8 * 1024 * 1024));
Channel server = null;
Channel client = null;
try {
server =
new ServerBootstrap()
.group(group)
.channel(NativeServerSocketChannel.class)
.childHandler(
new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
((NativeSocketChannel) channel).tls(serverTls, null);
channel
.pipeline()
.addLast(
new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(
ChannelHandlerContext context, Object message) {
context.writeAndFlush(message);
}
});
}
})
.bind(new InetSocketAddress("127.0.0.1", 0))
.sync()
.channel();
CompletableFuture<Integer> reply = new CompletableFuture<>();
client =
new Bootstrap()
.group(group)
.channelFactory(() -> new NativeSocketChannel().tls(clientTls, "localhost"))
.handler(
new ChannelInboundHandlerAdapter() {
private int received;
private int value;
@Override
public void channelRead(ChannelHandlerContext context, Object message) {
ByteBuf bytes = (ByteBuf) message;
try {
while (bytes.isReadable()) {
if (received == 4) throw new AssertionError("excess echo bytes");
value = (value << 8) | bytes.readUnsignedByte();
if (++received == 4) reply.complete(value);
}
} finally {
bytes.release();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext context, Throwable error) {
reply.completeExceptionally(error);
}
})
.connect(server.localAddress())
.sync()
.channel();
((NativeSocketChannel) client).handshakeFuture().sync();
if (!"h2".equals(((NativeSocketChannel) client).applicationProtocol()))
throw new AssertionError("ALPN mismatch");
client.writeAndFlush(Unpooled.buffer(4).writeInt(42)).sync();
if (reply.get(5, TimeUnit.SECONDS) != 42) throw new AssertionError("native echo mismatch");
System.out.println("Native transport-owned TLS checks passed");
} finally {
if (client != null) client.close().syncUninterruptibly();
if (server != null) server.close().syncUninterruptibly();
group.shutdownGracefully(0, 5, TimeUnit.SECONDS).syncUninterruptibly();
clientTls.close();
serverTls.close();
}
}
}