|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +package org.xbill.DNS; |
| 3 | + |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.doReturn; |
| 7 | +import static org.mockito.Mockito.spy; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import io.vertx.core.Vertx; |
| 11 | +import io.vertx.core.datagram.DatagramSocket; |
| 12 | +import io.vertx.core.net.SocketAddress; |
| 13 | +import io.vertx.junit5.VertxExtension; |
| 14 | +import io.vertx.junit5.VertxTestContext; |
| 15 | +import java.io.EOFException; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.InetSocketAddress; |
| 18 | +import java.nio.ByteBuffer; |
| 19 | +import java.nio.channels.DatagramChannel; |
| 20 | +import java.nio.channels.SelectionKey; |
| 21 | +import java.nio.channels.Selector; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.MockedStatic; |
| 31 | +import org.mockito.Mockito; |
| 32 | + |
| 33 | +@ExtendWith(VertxExtension.class) |
| 34 | +@SuppressWarnings("unchecked") |
| 35 | +class NioUdpClientTest { |
| 36 | + private static SocketAddress localAddress; |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + static void beforeAll(Vertx vertx, VertxTestContext context) { |
| 40 | + DatagramSocket datagramSocket = vertx.createDatagramSocket(); |
| 41 | + datagramSocket.handler( |
| 42 | + p -> datagramSocket.send(p.data(), p.sender().port(), p.sender().host())); |
| 43 | + datagramSocket |
| 44 | + .listen(0, "localhost") |
| 45 | + .map( |
| 46 | + s -> { |
| 47 | + localAddress = s.localAddress(); |
| 48 | + return null; |
| 49 | + }) |
| 50 | + .onComplete(context.succeedingThenComplete()); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterAll |
| 54 | + static void afterAll() { |
| 55 | + NioClient.close(); |
| 56 | + } |
| 57 | + |
| 58 | + private CompletableFuture<byte[]> createAndSendQuery() { |
| 59 | + NioUdpClient udp = new NioUdpClient(); |
| 60 | + Message query = Message.newQuery(Record.newRecord(Name.root, Type.A, DClass.IN)); |
| 61 | + return udp.sendAndReceiveUdp( |
| 62 | + null, |
| 63 | + new InetSocketAddress(localAddress.hostAddress(), localAddress.port()), |
| 64 | + query, |
| 65 | + query.toWire(), |
| 66 | + 65535, |
| 67 | + Duration.ofSeconds(10)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void selectorWithAllCanceledKey() throws IOException { |
| 72 | + Selector spiedSelector = spy(Selector.open()); |
| 73 | + when(spiedSelector.selectedKeys()) |
| 74 | + .thenAnswer( |
| 75 | + a -> { |
| 76 | + Set<SelectionKey> keys = (Set<SelectionKey>) a.callRealMethod(); |
| 77 | + for (SelectionKey key : keys) { |
| 78 | + key.cancel(); |
| 79 | + } |
| 80 | + return keys; |
| 81 | + }); |
| 82 | + |
| 83 | + try (MockedStatic<Selector> sel = Mockito.mockStatic(Selector.class)) { |
| 84 | + sel.when(Selector::open).thenReturn(spiedSelector); |
| 85 | + CompletableFuture<byte[]> result = createAndSendQuery(); |
| 86 | + assertThatThrownBy(result::get).hasCauseInstanceOf(EOFException.class); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void readFromKeyFailsFuture() throws IOException { |
| 92 | + Selector spiedSelector = spy(Selector.open()); |
| 93 | + when(spiedSelector.selectedKeys()) |
| 94 | + .thenAnswer( |
| 95 | + selectedKeysIntercept -> { |
| 96 | + Set<SelectionKey> keys = (Set<SelectionKey>) selectedKeysIntercept.callRealMethod(); |
| 97 | + Set<SelectionKey> mockedKeys = new HashSet<>(keys.size()); |
| 98 | + for (SelectionKey key : keys) { |
| 99 | + SelectionKey spy = spy(key); |
| 100 | + when(spy.channel()) |
| 101 | + .thenAnswer( |
| 102 | + channelIntercept -> { |
| 103 | + DatagramChannel channel = |
| 104 | + (DatagramChannel) channelIntercept.callRealMethod(); |
| 105 | + DatagramChannel spyChannel = spy(channel); |
| 106 | + doReturn(0).when(spyChannel).read(any(ByteBuffer.class)); |
| 107 | + return spyChannel; |
| 108 | + }); |
| 109 | + mockedKeys.add(spy); |
| 110 | + } |
| 111 | + |
| 112 | + return mockedKeys; |
| 113 | + }); |
| 114 | + |
| 115 | + try (MockedStatic<Selector> sel = Mockito.mockStatic(Selector.class)) { |
| 116 | + sel.when(Selector::open).thenReturn(spiedSelector); |
| 117 | + CompletableFuture<byte[]> result = createAndSendQuery(); |
| 118 | + assertThatThrownBy(result::get) |
| 119 | + .cause() |
| 120 | + .isInstanceOf(EOFException.class) |
| 121 | + .hasMessageStartingWith("Could not read expected data"); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments