Skip to content

Commit 943fd8c

Browse files
committed
add SSLEngine test case with split input across ByteBuffers
1 parent a490b2a commit 943fd8c

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

src/test/com/wolfssl/provider/jsse/test/WolfSSLEngineTest.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,5 +883,115 @@ public void testSSLEngineBigInput() throws Exception {
883883
}
884884
pass("\t... passed");
885885
}
886+
887+
@Test
888+
public void testSSLEngineSplitInput() throws Exception {
889+
890+
int appBufMax, netBufMax;
891+
int done = 0;
892+
ByteBuffer cIn;
893+
ByteBuffer cOut1;
894+
ByteBuffer cOut2;
895+
ByteBuffer[] cOutBuffs = new ByteBuffer[2];
896+
ByteBuffer[] sOutBuffs = new ByteBuffer[2];
897+
ByteBuffer sIn;
898+
ByteBuffer sOut1;
899+
ByteBuffer sOut2;
900+
ByteBuffer clientToServer;
901+
ByteBuffer serverToClient;
902+
903+
byte[] input1Buf = "Hello client, ".getBytes();
904+
byte[] input2Buf = "from server".getBytes();
905+
906+
SSLEngineResult cResult;
907+
SSLEngineResult sResult;
908+
909+
System.out.print("\tTesting split input data");
910+
911+
try {
912+
/* create SSLContext */
913+
this.ctx = tf.createSSLContext("TLS", engineProvider);
914+
915+
/* create server SSLEngine */
916+
SSLEngine server = this.ctx.createSSLEngine();
917+
server.setUseClientMode(false);
918+
server.setNeedClientAuth(true);
919+
920+
/* create client SSLEngine */
921+
SSLEngine client = this.ctx.createSSLEngine(
922+
"wolfSSL client test", 11111);
923+
client.setUseClientMode(true);
924+
925+
SSLSession session = client.getSession();
926+
appBufMax = session.getApplicationBufferSize();
927+
netBufMax = session.getPacketBufferSize();
928+
929+
cIn = ByteBuffer.allocate(appBufMax);
930+
sIn = ByteBuffer.allocate(netBufMax);
931+
clientToServer = ByteBuffer.allocate(netBufMax);
932+
serverToClient = ByteBuffer.allocate(netBufMax);
933+
934+
/* Input data split across 2 ByteBuffers on both cli and svr */
935+
cOut1 = ByteBuffer.wrap("Hello server, ".getBytes());
936+
cOut2 = ByteBuffer.wrap("from client".getBytes());
937+
cOutBuffs[0] = cOut1;
938+
cOutBuffs[1] = cOut2;
939+
940+
sOut1 = ByteBuffer.wrap("Hello client, ".getBytes());
941+
sOut2 = ByteBuffer.wrap("from server".getBytes());
942+
sOutBuffs[0] = sOut1;
943+
sOutBuffs[1] = sOut2;
944+
945+
while (!(client.isOutboundDone() && client.isInboundDone()) &&
946+
!(server.isOutboundDone() && server.isInboundDone())) {
947+
948+
cResult = client.wrap(cOutBuffs, clientToServer);
949+
sResult = server.wrap(sOutBuffs, serverToClient);
950+
951+
clientToServer.flip();
952+
serverToClient.flip();
953+
954+
cResult = client.unwrap(serverToClient, cIn);
955+
sResult = server.unwrap(clientToServer, sIn);
956+
957+
clientToServer.compact();
958+
serverToClient.compact();
959+
960+
if (done == 0 &&
961+
((cOut1.limit() + cOut2.limit()) == sIn.position()) &&
962+
((sOut1.limit() + sOut2.limit()) == cIn.position())) {
963+
964+
/* check server out matches client in */
965+
ByteBuffer cExpectedIn = ByteBuffer.wrap(
966+
"Hello client, from server".getBytes());
967+
cIn.flip();
968+
969+
if (!cIn.equals(cExpectedIn)) {
970+
error("\t... failed");
971+
fail("server output does not match expected");
972+
}
973+
974+
/* check client out matches server in */
975+
ByteBuffer sExpectedIn = ByteBuffer.wrap(
976+
"Hello server, from client".getBytes());
977+
sIn.flip();
978+
979+
if (!sIn.equals(sExpectedIn)) {
980+
error("\t... failed");
981+
fail("client output does not match expected");
982+
}
983+
984+
/* close client outbound, mark done */
985+
client.closeOutbound();
986+
done = 1;
987+
}
988+
}
989+
} catch (Exception e) {
990+
error("\t... failed");
991+
e.printStackTrace();
992+
fail("failed split input test with Exception");
993+
}
994+
pass("\t... passed");
995+
}
886996
}
887997

0 commit comments

Comments
 (0)