Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/jdk/net/ExtendedSocketOption/DatagramChannelNAPITest.java
42940 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8243099
27
* @library /test/lib
28
* @modules jdk.net
29
* @summary Check ExtendedSocketOption NAPI_ID support for DatagramChannel
30
* @run testng DatagramChannelNAPITest
31
* @run testng/othervm -Djava.net.preferIPv4Stack=true DatagramChannelNAPITest
32
*/
33
34
import jdk.test.lib.net.IPSupport;
35
import org.testng.SkipException;
36
import org.testng.annotations.BeforeTest;
37
import org.testng.annotations.Test;
38
39
import java.io.IOException;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.SocketException;
43
import java.nio.ByteBuffer;
44
import java.nio.channels.DatagramChannel;
45
46
import static org.testng.Assert.assertEquals;
47
import static org.testng.Assert.assertThrows;
48
import static org.testng.Assert.assertTrue;
49
import static jdk.net.ExtendedSocketOptions.SO_INCOMING_NAPI_ID;
50
51
public class DatagramChannelNAPITest {
52
private InetAddress hostAddr;
53
private static final Class<SocketException> SE = SocketException.class;
54
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
55
private static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
56
57
@BeforeTest
58
public void setup() throws IOException {
59
IPSupport.throwSkippedExceptionIfNonOperational();
60
try (var dc = DatagramChannel.open()) {
61
if (!dc.supportedOptions().contains(SO_INCOMING_NAPI_ID)) {
62
assertThrows(UOE, () -> dc.getOption(SO_INCOMING_NAPI_ID));
63
assertThrows(UOE, () -> dc.setOption(SO_INCOMING_NAPI_ID, 42));
64
assertThrows(UOE, () -> dc.setOption(SO_INCOMING_NAPI_ID, null));
65
throw new SkipException("NAPI ID not supported on this system");
66
}
67
}
68
hostAddr = InetAddress.getLocalHost();
69
}
70
71
@Test
72
public void testSetGetOptionDatagramChannel() throws IOException {
73
try (var dc = DatagramChannel.open()) {
74
assertEquals((int) dc.getOption(SO_INCOMING_NAPI_ID), 0);
75
assertThrows(SE, () -> dc.setOption(SO_INCOMING_NAPI_ID, 42));
76
assertThrows(IAE, () -> dc.setOption(SO_INCOMING_NAPI_ID, null));
77
}
78
}
79
80
@Test
81
public void testDatagramChannel() throws Exception {
82
int senderID, receiverID, tempID = 0;
83
boolean initialRun = true;
84
try (var r = DatagramChannel.open()) {
85
r.bind(new InetSocketAddress(hostAddr, 0));
86
var port = r.socket().getLocalPort();
87
var addr = new InetSocketAddress(hostAddr, port);
88
89
try (var s = DatagramChannel.open()) {
90
s.bind(null);
91
for (int i = 0; i < 10; i++) {
92
s.send(ByteBuffer.wrap("test".getBytes()), addr);
93
senderID = s.getOption(SO_INCOMING_NAPI_ID);
94
assertEquals(senderID, 0, "DatagramChannel: Sender");
95
96
r.receive(ByteBuffer.allocate(128));
97
receiverID = r.getOption(SO_INCOMING_NAPI_ID);
98
99
// check ID remains consistent
100
if (initialRun) {
101
assertTrue(receiverID >= 0, "DatagramChannel: Receiver");
102
} else {
103
assertEquals(receiverID, tempID);
104
initialRun = false;
105
}
106
tempID = receiverID;
107
}
108
}
109
}
110
}
111
}
112
113