Repository navigation
Expand file tree
/
Copy pathNativeReentrantCloseTest.java
More file actions
272 lines (262 loc) Β· 11.7 KB
/
Copy pathNativeReentrantCloseTest.java
File metadata and controls
272 lines (262 loc) Β· 11.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/** Callback closure and deregistration must not orphan TLS sessions. */
public final class NativeReentrantCloseTest {
public static void main(String[] args) throws Exception {
verify(
new BackendTransport(new FfmTransportNative(Path.of(args[0]))),
Files.readAllBytes(Path.of(args[1])),
Files.readAllBytes(Path.of(args[2])));
}
public static void verify(TransportNative delegate, byte[] cert, byte[] key) throws Exception {
AtomicInteger sessions = new AtomicInteger();
AtomicInteger created = new AtomicInteger();
TransportNative api =
new BackendTransport(delegate) {
@Override
public long tlsNew(long workload, long context, long owner, long name) {
long session = super.tlsNew(workload, context, owner, name);
if (session != 0) {
created.incrementAndGet();
sessions.incrementAndGet();
}
return session;
}
@Override
public int tlsRelease(long session) {
int result = super.tlsRelease(session);
if (result == 0) sessions.decrementAndGet();
return result;
}
};
try (NativeTlsContext clientTls = NativeTlsContext.client(api, cert, "h2");
NativeTlsContext serverTls = NativeTlsContext.server(api, cert, key, "h2")) {
EventLoopGroup group =
new MultiThreadIoEventLoopGroup(
1, NativeIoHandler.newFactory(api, 0, 128, 8 * 1024 * 1024));
Channel server = null;
Channel client = null;
try {
AtomicInteger active = new AtomicInteger();
AtomicInteger connected = new AtomicInteger();
try (ServerSocket listener = new ServerSocket(0, 16, InetAddress.getLoopbackAddress())) {
client =
new Bootstrap()
.group(group)
.channelFactory(() -> new NativeSocketChannel().tls(clientTls, "localhost"))
.handler(
new ChannelDuplexHandler() {
@Override
public void connect(
ChannelHandlerContext ctx,
SocketAddress remote,
SocketAddress local,
ChannelPromise promise) {
promise.addListener(
done -> {
connected.incrementAndGet();
ctx.writeAndFlush(Unpooled.buffer(1).writeByte(7));
ctx.read();
ctx.close();
});
ctx.connect(remote, local, promise);
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
active.incrementAndGet();
ctx.fireChannelActive();
}
})
.connect(listener.getLocalSocketAddress())
.sync()
.channel();
try (Socket peer = listener.accept()) {
client.closeFuture().sync();
client.eventLoop().submit(() -> {}).sync();
if (created.get() != 0 || active.get() != 0 || connected.get() != 1)
throw new AssertionError("Connect listener close still started TLS or fired active");
var closedHandshake = ((NativeSocketChannel) client).handshakeFuture();
if (!closedHandshake.await(1, TimeUnit.SECONDS)
|| !(closedHandshake.cause() instanceof java.nio.channels.ClosedChannelException))
throw new AssertionError("Closed channel left a pending or successful handshake");
if (created.get() != 0)
throw new AssertionError("Closed handshake created a TLS session");
peer.setSoTimeout(3000);
if (peer.getInputStream().read() != -1)
throw new AssertionError("Closed TLS connect sent bytes");
}
}
CompletableFuture<Void> received = new CompletableFuture<>();
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 ctx, Object message) {
ByteBuf bytes = (ByteBuf) message;
try {
if (bytes.readableBytes() != 1 || bytes.readByte() != 7) {
received.completeExceptionally(
new AssertionError("Connect listener write mismatch"));
} else received.complete(null);
} finally {
bytes.release();
}
}
@Override
public void exceptionCaught(
ChannelHandlerContext ctx, Throwable cause) {
received.completeExceptionally(cause);
ctx.close();
}
});
}
})
.bind(new InetSocketAddress("127.0.0.1", 0))
.sync()
.channel();
client =
new Bootstrap()
.group(group)
.channelFactory(() -> new NativeSocketChannel().tls(clientTls, "localhost"))
.handler(
new ChannelDuplexHandler() {
@Override
public void connect(
ChannelHandlerContext ctx,
SocketAddress remote,
SocketAddress local,
ChannelPromise promise) {
promise.addListener(
done -> {
ctx.writeAndFlush(Unpooled.buffer(1).writeByte(7));
ctx.read();
});
ctx.connect(remote, local, promise);
}
})
.connect(server.localAddress())
.sync()
.channel();
((NativeSocketChannel) client).handshakeFuture().sync();
received.get(5, TimeUnit.SECONDS);
client.deregister().sync();
if (client.isOpen() || !client.closeFuture().isDone())
throw new AssertionError(
"TLS deregistration deferred close after detaching completion delivery");
} finally {
if (client != null) client.close();
if (server != null) server.close();
group.shutdownGracefully(0, 5, TimeUnit.SECONDS).syncUninterruptibly();
}
shutdownLiveTls(api, clientTls, serverTls);
if (sessions.get() != 0) throw new AssertionError("Orphaned TLS sessions: " + sessions.get());
}
System.out.println("Native callback closure and TLS deregistration checks passed");
}
private static void shutdownLiveTls(
TransportNative api, NativeTlsContext clientTls, NativeTlsContext serverTls)
throws Exception {
EventLoopGroup group =
new MultiThreadIoEventLoopGroup(
1, NativeIoHandler.newFactory(api, 0, 128, 8 * 1024 * 1024));
AtomicInteger inactive = new AtomicInteger();
AtomicInteger unregistered = new AtomicInteger();
AtomicBoolean lateCallback = new AtomicBoolean();
CompletableFuture<Channel> accepted = new CompletableFuture<>();
try {
Channel 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(lifecycle(inactive, unregistered, lateCallback));
accepted.complete(channel);
}
})
.bind(new InetSocketAddress("127.0.0.1", 0))
.sync()
.channel();
Channel client =
new Bootstrap()
.group(group)
.channelFactory(() -> new NativeSocketChannel().tls(clientTls, "localhost"))
.handler(lifecycle(inactive, unregistered, lateCallback))
.connect(server.localAddress())
.sync()
.channel();
Channel peer = accepted.get(5, TimeUnit.SECONDS);
((NativeSocketChannel) client).handshakeFuture().sync();
((NativeSocketChannel) peer).handshakeFuture().sync();
// Flip shutdown while draining tasks, after the loop's initial shutdown check.
group.next().execute(() -> group.shutdownGracefully(0, 5, TimeUnit.SECONDS));
if (!group.terminationFuture().await(10, TimeUnit.SECONDS))
throw new AssertionError("Live TLS shutdown did not terminate");
if (client.isOpen()
|| peer.isOpen()
|| server.isOpen()
|| client.isRegistered()
|| peer.isRegistered()
|| server.isRegistered())
throw new AssertionError("Shutdown retained a live or registered TLS channel");
if (inactive.get() != 2 || unregistered.get() != 2 || lateCallback.get())
throw new AssertionError(
"Shutdown lost or delayed TLS lifecycle callbacks: inactive="
+ inactive.get()
+ ", unregistered="
+ unregistered.get());
} finally {
group.shutdownGracefully(0, 5, TimeUnit.SECONDS).syncUninterruptibly();
}
}
private static ChannelHandler lifecycle(
AtomicInteger inactive, AtomicInteger unregistered, AtomicBoolean lateCallback) {
return new ChannelInboundHandlerAdapter() {
@Override
public void channelInactive(ChannelHandlerContext ctx) {
if (ctx.executor().isTerminated()) lateCallback.set(true);
inactive.incrementAndGet();
ctx.fireChannelInactive();
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) {
if (ctx.executor().isTerminated()) lateCallback.set(true);
unregistered.incrementAndGet();
ctx.fireChannelUnregistered();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.close();
}
};
}
}