|
17 | 17 |
|
18 | 18 | import io.netty.bootstrap.Bootstrap; |
19 | 19 | import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufHolder; |
20 | 21 | import io.netty.buffer.CompositeByteBuf; |
21 | 22 | import io.netty.buffer.Unpooled; |
22 | 23 | import io.netty.channel.Channel; |
|
33 | 34 | import io.netty.util.internal.EmptyArrays; |
34 | 35 | import org.junit.jupiter.api.Test; |
35 | 36 | import org.junit.jupiter.api.TestInfo; |
| 37 | +import org.junit.jupiter.api.Timeout; |
36 | 38 | import org.opentest4j.TestAbortedException; |
37 | 39 |
|
38 | 40 | import java.net.BindException; |
|
44 | 46 | import java.net.SocketException; |
45 | 47 | import java.nio.channels.NotYetConnectedException; |
46 | 48 | import java.nio.channels.UnresolvedAddressException; |
| 49 | +import java.nio.charset.StandardCharsets; |
47 | 50 | import java.util.ArrayList; |
48 | 51 | import java.util.List; |
| 52 | +import java.util.concurrent.CompletableFuture; |
49 | 53 | import java.util.concurrent.CountDownLatch; |
50 | 54 | import java.util.concurrent.Semaphore; |
51 | 55 | import java.util.concurrent.TimeUnit; |
52 | 56 | import java.util.concurrent.atomic.AtomicReference; |
53 | 57 |
|
54 | 58 | import static org.assertj.core.api.Assumptions.assumeThat; |
| 59 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 60 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
55 | 61 | import static org.junit.jupiter.api.Assertions.assertFalse; |
56 | 62 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
57 | 63 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
61 | 67 |
|
62 | 68 | public abstract class DatagramUnicastTest extends AbstractDatagramTest { |
63 | 69 |
|
| 70 | + private static final byte[] BAD_PREFIX = "BAD-PREFIX-".getBytes(StandardCharsets.UTF_8); |
| 71 | + private static final byte[] EXPECTED = "EXPECTED-direct-reader-index".getBytes(StandardCharsets.UTF_8); |
64 | 72 | private static final byte[] BYTES = {0, 1, 2, 3}; |
65 | 73 | protected enum WrapType { |
66 | 74 | NONE, DUP, SLICE, READ_ONLY |
@@ -236,7 +244,7 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throw |
236 | 244 | throw e; |
237 | 245 | } |
238 | 246 | SocketAddress sendAddress = address instanceof InetSocketAddress ? |
239 | | - sendToAddress((InetSocketAddress) address) : address; |
| 247 | + convertAnyAddress((InetSocketAddress) address) : address; |
240 | 248 | for (int i = 0; i < 100; i++) { |
241 | 249 | try { |
242 | 250 | client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress)); |
@@ -277,7 +285,7 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) { |
277 | 285 | Channel cc = cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true). |
278 | 286 | handler(new ChannelInboundHandlerAdapter()).register().sync().channel(); |
279 | 287 | try { |
280 | | - InetSocketAddress goodHost = sendToAddress((InetSocketAddress) sc.localAddress()); |
| 288 | + InetSocketAddress goodHost = convertAnyAddress((InetSocketAddress) sc.localAddress()); |
281 | 289 | InetSocketAddress unresolvedHost = new InetSocketAddress("NOT_A_REAL_ADDRESS", goodHost.getPort()); |
282 | 290 |
|
283 | 291 | assertFalse(goodHost.isUnresolved()); |
@@ -333,7 +341,7 @@ public void channelRead0(ChannelHandlerContext ctx, Object msgs) { |
333 | 341 |
|
334 | 342 | SocketAddress localAddr = sc.localAddress(); |
335 | 343 | SocketAddress addr = localAddr instanceof InetSocketAddress ? |
336 | | - sendToAddress((InetSocketAddress) sc.localAddress()) : localAddr; |
| 344 | + convertAnyAddress((InetSocketAddress) sc.localAddress()) : localAddr; |
337 | 345 | List<ChannelFuture> futures = new ArrayList<ChannelFuture>(count); |
338 | 346 | for (int i = 0; i < count; i++) { |
339 | 347 | futures.add(write(cc, buf, addr, wrapType)); |
@@ -385,7 +393,7 @@ private void testSimpleSendWithConnect0(Bootstrap sb, Bootstrap cb, ByteBuf buf, |
385 | 393 |
|
386 | 394 | SocketAddress localAddr = sc.localAddress(); |
387 | 395 | SocketAddress addr = localAddr instanceof InetSocketAddress ? |
388 | | - sendToAddress((InetSocketAddress) sc.localAddress()) : localAddr; |
| 396 | + convertAnyAddress((InetSocketAddress) sc.localAddress()) : localAddr; |
389 | 397 | cc.connect(addr).syncUninterruptibly(); |
390 | 398 |
|
391 | 399 | List<ChannelFuture> futures = new ArrayList<ChannelFuture>(); |
@@ -468,6 +476,80 @@ private static ChannelFuture write(Channel cc, ByteBuf buf, WrapType wrapType) { |
468 | 476 | } |
469 | 477 | } |
470 | 478 |
|
| 479 | + @Test |
| 480 | + @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS) |
| 481 | + public void testWriteOffsetBytebuf(TestInfo testInfo) throws Throwable { |
| 482 | + run(testInfo, new Runner<Bootstrap, Bootstrap>() { |
| 483 | + @Override |
| 484 | + public void run(Bootstrap sb, Bootstrap cb) throws Throwable { |
| 485 | + testWriteOffsetBytebuf(sb, cb); |
| 486 | + } |
| 487 | + }); |
| 488 | + } |
| 489 | + |
| 490 | + private void testWriteOffsetBytebuf(Bootstrap sb, Bootstrap cb) throws Throwable { |
| 491 | + ByteBuf buf = Unpooled.directBuffer(BAD_PREFIX.length + EXPECTED.length); |
| 492 | + buf.writeBytes(BAD_PREFIX); |
| 493 | + buf.writeBytes(EXPECTED); |
| 494 | + buf.readerIndex(BAD_PREFIX.length); |
| 495 | + try { |
| 496 | + for (WrapType type : WrapType.values()) { |
| 497 | + testWriteOffsetBytebuf0(sb, cb, buf.retain(), EXPECTED, type); |
| 498 | + } |
| 499 | + assertEquals(1, buf.refCnt()); |
| 500 | + } finally { |
| 501 | + buf.release(); |
| 502 | + } |
| 503 | + } |
| 504 | + |
| 505 | + private void testWriteOffsetBytebuf0(Bootstrap sb, Bootstrap cb, ByteBuf buf, byte[] expected, |
| 506 | + WrapType wrapType) throws Throwable { |
| 507 | + CompletableFuture<byte[]> received = new CompletableFuture<>(); |
| 508 | + sb.handler(new SimpleChannelInboundHandler<ByteBufHolder>() { |
| 509 | + @Override |
| 510 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder packet) { |
| 511 | + ByteBuf content = packet.content(); |
| 512 | + byte[] bytes = new byte[content.readableBytes()]; |
| 513 | + content.getBytes(content.readerIndex(), bytes); |
| 514 | + received.complete(bytes); |
| 515 | + } |
| 516 | + }); |
| 517 | + |
| 518 | + Channel sc = null; |
| 519 | + Channel cc = null; |
| 520 | + try { |
| 521 | + sc = sb.bind(newSocketAddress()).sync().channel(); |
| 522 | + SocketAddress serverAddress = sc.localAddress(); |
| 523 | + if (serverAddress instanceof InetSocketAddress) { |
| 524 | + // Correctly ha |
| 525 | + serverAddress = convertAnyAddress((InetSocketAddress) serverAddress); |
| 526 | + } |
| 527 | + cb.handler(new SimpleChannelInboundHandler<ByteBufHolder>() { |
| 528 | + @Override |
| 529 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) { |
| 530 | + // no-op |
| 531 | + } |
| 532 | + }); |
| 533 | + |
| 534 | + cc = cb.connect(serverAddress).sync().channel(); |
| 535 | + ChannelFuture wf = write(cc, buf, wrapType); |
| 536 | + cc.flush(); |
| 537 | + wf.sync(); |
| 538 | + byte[] actual = received.join(); |
| 539 | + assertArrayEquals(expected, actual); |
| 540 | + } finally { |
| 541 | + // release as we used buf.retain() before |
| 542 | + buf.release(); |
| 543 | + |
| 544 | + if (cc != null) { |
| 545 | + cc.close().sync(); |
| 546 | + } |
| 547 | + if (sc != null) { |
| 548 | + sc.close().sync(); |
| 549 | + } |
| 550 | + } |
| 551 | + } |
| 552 | + |
471 | 553 | protected abstract boolean isConnected(Channel channel); |
472 | 554 |
|
473 | 555 | protected abstract Channel setupClientChannel(Bootstrap cb, byte[] bytes, CountDownLatch latch, |
@@ -491,7 +573,7 @@ protected static void closeChannel(Channel channel) throws Exception { |
491 | 573 | } |
492 | 574 | } |
493 | 575 |
|
494 | | - protected InetSocketAddress sendToAddress(InetSocketAddress serverAddress) { |
| 576 | + protected InetSocketAddress convertAnyAddress(InetSocketAddress serverAddress) { |
495 | 577 | InetAddress addr = serverAddress.getAddress(); |
496 | 578 | if (addr.isAnyLocalAddress()) { |
497 | 579 | if (addr instanceof Inet6Address) { |
|