-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathNioUdpClientTest.java
More file actions
135 lines (123 loc) · 4.7 KB
/
Copy pathNioUdpClientTest.java
File metadata and controls
135 lines (123 loc) · 4.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
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import io.vertx.core.Vertx;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.core.net.SocketAddress;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.xbill.DNS.io.DefaultIoClientFactory;
import org.xbill.DNS.io.UdpIoClient;
@ExtendWith(VertxExtension.class)
@SuppressWarnings("unchecked")
@EnabledIf("notWindowsOrJre17Plus")
class NioUdpClientTest {
private static SocketAddress localAddress;
@SuppressWarnings("java:S1144")
private static boolean notWindowsOrJre17Plus() {
String javaVersion = System.getProperty("java.version", "0");
return !System.getProperty("os.name").contains("Windows")
|| Integer.parseInt(javaVersion.substring(0, javaVersion.indexOf('.'))) >= 17;
}
@BeforeAll
static void beforeAll(Vertx vertx, VertxTestContext context) {
DatagramSocket datagramSocket = vertx.createDatagramSocket();
datagramSocket.handler(
p -> datagramSocket.send(p.data(), p.sender().port(), p.sender().host()));
datagramSocket
.listen(0, "localhost")
.map(
s -> {
localAddress = s.localAddress();
return null;
})
.onComplete(context.succeedingThenComplete());
}
@AfterAll
static void afterAll() {
NioClient.close();
}
private CompletableFuture<byte[]> createAndSendQuery() {
UdpIoClient udp = new DefaultIoClientFactory().createOrGetUdpClient();
Message query = Message.newQuery(Record.newRecord(Name.root, Type.A, DClass.IN));
return udp.sendAndReceiveUdp(
null,
new InetSocketAddress(localAddress.hostAddress(), localAddress.port()),
query,
query.toWire(),
65535,
Duration.ofSeconds(10));
}
@Test
void selectorWithAllCanceledKey() throws IOException {
Selector spiedSelector = spy(Selector.open());
when(spiedSelector.selectedKeys())
.thenAnswer(
a -> {
Set<SelectionKey> keys = (Set<SelectionKey>) a.callRealMethod();
for (SelectionKey key : keys) {
key.cancel();
}
return keys;
});
try (MockedStatic<Selector> sel = Mockito.mockStatic(Selector.class)) {
sel.when(Selector::open).thenReturn(spiedSelector);
CompletableFuture<byte[]> result = createAndSendQuery();
assertThatThrownBy(result::get).hasCauseInstanceOf(EOFException.class);
}
}
@Test
void readFromKeyFailsFuture() throws IOException {
Selector spiedSelector = spy(Selector.open());
when(spiedSelector.selectedKeys())
.thenAnswer(
selectedKeysIntercept -> {
Set<SelectionKey> keys = (Set<SelectionKey>) selectedKeysIntercept.callRealMethod();
Set<SelectionKey> mockedKeys = new HashSet<>(keys.size());
for (SelectionKey key : keys) {
SelectionKey spy = spy(key);
when(spy.channel())
.thenAnswer(
channelIntercept -> {
DatagramChannel channel =
(DatagramChannel) channelIntercept.callRealMethod();
DatagramChannel spyChannel = spy(channel);
doReturn(0).when(spyChannel).read(any(ByteBuffer.class));
return spyChannel;
});
mockedKeys.add(spy);
}
return mockedKeys;
});
try (MockedStatic<Selector> sel = Mockito.mockStatic(Selector.class)) {
sel.when(Selector::open).thenReturn(spiedSelector);
CompletableFuture<byte[]> result = createAndSendQuery();
assertThatThrownBy(result::get)
.cause()
.isInstanceOf(EOFException.class)
.hasMessageStartingWith("Could not read expected data");
}
}
}