Skip to content

Commit 5d3ba60

Browse files
committed
add tests
1 parent 46d31cd commit 5d3ba60

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/main/java/net/lecousin/framework/network/ssl/SSLLayer.java

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ private void checkHandshakeTasks(SSLEngine engine) {
249249
private void needTask() {
250250
@SuppressWarnings("unchecked")
251251
List<Task<Void,NoException>> tasks = (List<Task<Void,NoException>>)conn.getAttribute(HANDSHAKE_TASKS_ATTRIBUTE);
252-
if (tasks == null || tasks.isEmpty()) {
253-
followup(conn, timeout);
254-
return;
255-
}
256252
MutableBoolean firstTask = new MutableBoolean(true);
257253
Runnable taskDone = () -> {
258254
synchronized (firstTask) {
@@ -265,15 +261,8 @@ private void needTask() {
265261
return null;
266262
}).start();
267263
};
268-
for (Iterator<Task<Void,NoException>> it = tasks.iterator(); it.hasNext(); ) {
269-
Task<Void,NoException> t = it.next();
270-
if (t.getOutput().isDone()) {
271-
it.remove();
272-
followup(conn, timeout);
273-
return;
274-
}
275-
t.getOutput().onDone(taskDone);
276-
}
264+
for (Iterator<Task<Void,NoException>> it = tasks.iterator(); it.hasNext(); )
265+
it.next().getOutput().onDone(taskDone);
277266
}
278267

279268
private void needWrap() {
@@ -334,14 +323,7 @@ private boolean needUnwrap(SSLEngine engine, ByteBuffer inputBuffer, Object inpu
334323
return true;
335324
}
336325
if (SSLEngineResult.Status.BUFFER_OVERFLOW.equals(result.getStatus())) {
337-
if (logger.debug())
338-
logger.debug(
339-
"Cannot unwrap because buffer is too small, enlarge it");
340-
ByteBuffer b = ByteBuffer.wrap(bufferCache.get(dst.capacity() << 1, true));
341-
dst.flip();
342-
b.put(dst);
343-
bufferCache.free(dst);
344-
dst = b;
326+
dst = enlargeBuffer(dst);
345327
continue;
346328
}
347329
if (logger.debug())
@@ -450,13 +432,7 @@ private void dataReceived(TCPConnection conn, SSLEngine engine, ByteBuffer input
450432
return;
451433
}
452434
if (SSLEngineResult.Status.BUFFER_OVERFLOW.equals(result.getStatus())) {
453-
if (logger.debug())
454-
logger.debug("Output buffer too small to decrypt data, try again with larger one");
455-
ByteBuffer b = ByteBuffer.wrap(bufferCache.get(dst.capacity() << 1, true));
456-
dst.flip();
457-
b.put(dst);
458-
bufferCache.free(dst);
459-
dst = b;
435+
dst = enlargeBuffer(dst);
460436
continue;
461437
}
462438
// data ready
@@ -480,6 +456,14 @@ private void compactInputBuffer(ByteBuffer inputBuffer, int packetSize, TCPConne
480456
inputBuffer.compact();
481457
}
482458

459+
private ByteBuffer enlargeBuffer(ByteBuffer buf) {
460+
ByteBuffer b = ByteBuffer.wrap(bufferCache.get(buf.capacity() << 1, true));
461+
buf.flip();
462+
b.put(buf);
463+
bufferCache.free(buf);
464+
return b;
465+
}
466+
483467
/** Encrypt the given data. */
484468
@SuppressWarnings("squid:S1319") // return LinkedList instead of List
485469
public LinkedList<ByteBuffer> encryptDataToSend(TCPConnection conn, List<ByteBuffer> data) throws SSLException {
@@ -512,11 +496,7 @@ public LinkedList<ByteBuffer> encryptDataToSend(TCPConnection conn, List<ByteBuf
512496
continue;
513497
}
514498
packetSize <<= 1;
515-
ByteBuffer b = ByteBuffer.wrap(bufferCache.get(dst.capacity() << 1, true));
516-
dst.flip();
517-
b.put(dst);
518-
bufferCache.free(dst);
519-
dst = b;
499+
dst = enlargeBuffer(dst);
520500
continue;
521501
}
522502
if (!toEncrypt.hasRemaining()) {

src/test/java/net/lecousin/framework/network/tests/tcp/TestSSLClient.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package net.lecousin.framework.network.tests.tcp;
22

3+
import java.io.IOException;
34
import java.net.InetAddress;
45
import java.net.InetSocketAddress;
56
import java.net.SocketAddress;
67
import java.nio.ByteBuffer;
78

89
import javax.net.ssl.SSLException;
910

11+
import net.lecousin.framework.concurrent.async.Async;
1012
import net.lecousin.framework.network.client.SSLClient;
1113
import net.lecousin.framework.network.client.TCPClient;
1214
import net.lecousin.framework.network.server.TCPServer;
1315
import net.lecousin.framework.network.server.protocol.SSLServerProtocol;
1416
import net.lecousin.framework.network.test.AbstractNetworkTest;
1517

18+
import org.junit.Assert;
1619
import org.junit.Test;
1720

1821
public class TestSSLClient extends AbstractNetworkTest {
@@ -84,4 +87,22 @@ public void testConnectWithHostname() throws Exception {
8487
}
8588
}
8689

90+
@Test
91+
public void testTunnelConnected() throws Exception {
92+
try (TCPServer server = new TCPServer()) {
93+
server.setProtocol(new SSLServerProtocol(sslTest, new WelcomeProtocol()));
94+
SocketAddress serverAddress = server.bind(new InetSocketAddress("localhost", 0), 0).blockResult(0);
95+
96+
try (SSLClient client = new SSLClient(sslTest)) {
97+
TCPClient tunnel = new TCPClient();
98+
tunnel.connect(serverAddress, 0).blockThrow(0);
99+
Async<IOException> connection = new Async<>();
100+
client.tunnelConnected(tunnel, connection, 10000);
101+
connection.blockThrow(0);
102+
client.send(ByteBuffer.wrap(new byte[] { 'B', 'y', 'e', '\n' }), 5000);
103+
Assert.assertArrayEquals(new byte[] { 'W', 'e', 'l', 'c', 'o', 'm', 'e', '\n', 'B', 'y', 'e' }, client.getReceiver().readBytes(11, 5000).blockResult(0));
104+
}
105+
}
106+
}
107+
87108
}

src/test/java/net/lecousin/framework/network/tests/tcp/TestTCPWelcomeProtocol.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.lecousin.framework.concurrent.async.Async;
1616
import net.lecousin.framework.concurrent.async.AsyncSupplier;
1717
import net.lecousin.framework.concurrent.async.IAsync;
18+
import net.lecousin.framework.concurrent.util.AsyncConsumer;
1819
import net.lecousin.framework.concurrent.util.PartialAsyncConsumer;
1920
import net.lecousin.framework.io.buffering.ByteArrayIO;
2021
import net.lecousin.framework.io.util.DataUtil;
@@ -304,6 +305,17 @@ public void testClientReceiverWithConsumer() throws Exception {
304305
}
305306
}
306307

308+
@Test
309+
public void testClientAsConsumer() throws Exception {
310+
try (TCPClient client = connectClient()) {
311+
AsyncConsumer<ByteBuffer, IOException> consumer = client.asConsumer(3, 5000);
312+
consumer.consume(ByteBuffer.wrap(new byte[] { 'B', 'y', 'e', '\n' }));
313+
consumer.end().blockThrow(0);
314+
consumer.error(new IOException("test"));
315+
Assert.assertArrayEquals(new byte[] { 'W', 'e', 'l', 'c', 'o', 'm', 'e', '\n', 'B', 'y', 'e' }, client.getReceiver().readBytes(11, 5000).blockResult(0));
316+
}
317+
}
318+
307319
@Test
308320
public void testFloodMe() throws Exception {
309321
Assert.assertEquals(0, server.getConnectedClients().size());

0 commit comments

Comments
 (0)