/
NativeSocketTest.java
206 lines (170 loc) · 7.78 KB
/
NativeSocketTest.java
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
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2015 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2015 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.jicmp.jna;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
/**
* NativeSocketTest
*
* @author brozow
*/
@Ignore
public class NativeSocketTest {
private static final ExecutorService m_executor = Executors.newCachedThreadPool();
Server m_server;
int m_port = 0;
@Rule
public TestName m_testName = new TestName();
@Before
public void setUp() throws Exception {
System.err.println("------------------- begin " + m_testName.getMethodName() + " ---------------------");
m_server = new Server(m_port);
m_server.start();
m_server.waitForStart();
m_port = m_server.getPort();
}
@After
public void tearDown() throws InterruptedException {
m_server.stop();
m_port = 0;
System.err.println("------------------- end " + m_testName.getMethodName() + " -----------------------");
}
public void printf(final String fmt, final Object... args) {
System.err.print(String.format(fmt, args));
}
@Test
public void testServer() throws Exception {
String[] cmds = new String[] { "echo", "echo2", "quit" };
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
for(final String cmd : cmds) {
final DatagramSocket sock = socket;
final FutureTask<DatagramPacket> task = new FutureTask<DatagramPacket>(new Callable<DatagramPacket>() {
@Override public DatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final byte[] data = cmd.getBytes(StandardCharsets.UTF_8);
final DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), sock.getLocalPort());
sock.send(p);
printf("Receiving...\n");
final DatagramPacket r = new DatagramPacket(new byte[128], 128);
sock.receive(r);
printf("Received\n");
return r;
}
});
m_executor.execute(task);
final DatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = new String(r.getData(), r.getOffset(), r.getLength(), StandardCharsets.UTF_8);
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
} finally {
if (socket != null) socket.close();
}
}
@Test
public void testNativeV4() throws Exception {
testNative(NativeDatagramSocket.PF_INET, InetAddress.getByName("127.0.0.1"));
}
@Test
public void testNativeV6() throws Exception {
testNative(NativeDatagramSocket.PF_INET6, InetAddress.getByName("::1"));
}
private void testNative(final int family, final InetAddress address) throws Exception {
final String[] cmds = new String[] { "nativeEcho", "nativeEcho2", "quitNative" };
NativeDatagramSocket socket = null;
try {
socket = NativeDatagramSocket.create(family, NativeDatagramSocket.IPPROTO_UDP, 1234);
for(final String cmd : cmds) {
final NativeDatagramSocket sock = socket;
final FutureTask<NativeDatagramPacket> task = new FutureTask<NativeDatagramPacket>(new Callable<NativeDatagramPacket>() {
@Override public NativeDatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final ByteBuffer buf = StandardCharsets.UTF_8.encode(cmd);
final NativeDatagramPacket p = new NativeDatagramPacket(buf, address, m_port);
sock.send(p);
printf("Receiving...\n");
final NativeDatagramPacket r = new NativeDatagramPacket(128);
sock.receive(r);
printf("Received.\n");
return r;
}
});
m_executor.execute(task);
final NativeDatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = StandardCharsets.UTF_8.decode(r.getContent()).toString();
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
} finally {
if (socket != null) socket.close();
}
}
@Test(timeout=10000)
@Ignore("This is ignored since I haven't found a way to interrupt a socket blocked on recvfrom in linux")
public void testCloseInReceive() throws Exception {
try(final NativeDatagramSocket socket = NativeDatagramSocket.create(NativeDatagramSocket.PF_INET, NativeDatagramSocket.IPPROTO_UDP, 1234)) {
final FutureTask<NativeDatagramPacket> task = new FutureTask<NativeDatagramPacket>(new Callable<NativeDatagramPacket>() {
@Override public NativeDatagramPacket call() throws Exception {
final ByteBuffer buf = StandardCharsets.UTF_8.encode("msg1");
final NativeDatagramPacket p = new NativeDatagramPacket(buf, InetAddress.getLocalHost(), m_port);
socket.send(p);
final NativeDatagramPacket r = new NativeDatagramPacket(128);
printf("Receiving...\n");
socket.receive(r);
printf("Received\n");
return r;
}
});
m_executor.execute(task);
final NativeDatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = StandardCharsets.UTF_8.decode(r.getContent()).toString();
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
}
}
}