Skip to content

Commit 56810a8

Browse files
committed
Add regression tests
1 parent 9c99f7d commit 56810a8

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import java.util.concurrent.CyclicBarrier;
7171
import java.util.concurrent.BrokenBarrierException;
7272
import java.util.concurrent.atomic.AtomicIntegerArray;
73+
import static org.junit.Assert.assertArrayEquals;
7374
import static org.junit.Assert.assertEquals;
7475
import static org.junit.Assert.assertNotNull;
7576
import static org.junit.Assert.fail;
@@ -3347,5 +3348,76 @@ public void testWrapPartialDrainOffsetUpdate()
33473348
fail("drained output does not match injected queue");
33483349
}
33493350
}
3351+
3352+
/* Regression: closeOutbound() before handshake must also close
3353+
* inbound, otherwise isInboundDone() never returns true. */
3354+
@Test
3355+
public void testCloseOutboundBeforeHandshake() throws Exception {
3356+
this.ctx = tf.createSSLContext("TLS", engineProvider);
3357+
SSLEngine e = this.ctx.createSSLEngine();
3358+
e.setUseClientMode(true);
3359+
e.closeOutbound();
3360+
assertTrue(e.isOutboundDone());
3361+
assertTrue(e.isInboundDone());
3362+
}
3363+
3364+
/* Regression for wrap(ByteBuffer[], ofst, len, out) when ofst > 0:
3365+
* pos[]/limit[] OOB and null-check loop bound. */
3366+
@Test
3367+
public void testWrapWithBufferArrayOffset() throws Exception {
3368+
this.ctx = tf.createSSLContext("TLS", engineProvider);
3369+
SSLEngine server = this.ctx.createSSLEngine();
3370+
SSLEngine client = this.ctx.createSSLEngine("wolfSSL test", 11111);
3371+
server.setUseClientMode(false);
3372+
client.setUseClientMode(true);
3373+
server.beginHandshake();
3374+
client.beginHandshake();
3375+
assertEquals(0, tf.testConnection(server, client, null, null, "x"));
3376+
3377+
byte[] payload = "real-payload".getBytes();
3378+
ByteBuffer[] in = {ByteBuffer.wrap("DECOY".getBytes()),
3379+
ByteBuffer.wrap(payload)};
3380+
ByteBuffer net = ByteBuffer.allocateDirect(
3381+
client.getSession().getPacketBufferSize());
3382+
3383+
SSLEngineResult r = client.wrap(in, 1, 1, net);
3384+
assertEquals(SSLEngineResult.Status.OK, r.getStatus());
3385+
assertEquals(0, in[0].position());
3386+
assertEquals(payload.length, in[1].position());
3387+
3388+
net.flip();
3389+
ByteBuffer plain = ByteBuffer.allocate(
3390+
server.getSession().getApplicationBufferSize());
3391+
assertEquals(SSLEngineResult.Status.OK,
3392+
server.unwrap(net, plain).getStatus());
3393+
plain.flip();
3394+
byte[] got = new byte[plain.remaining()];
3395+
plain.get(got);
3396+
assertArrayEquals(payload, got);
3397+
}
3398+
3399+
/* Direct regression: wrap() null-check must reach in[ofst+len-1]. */
3400+
@Test(expected = SSLException.class)
3401+
public void testWrapRejectsNullAtOffset() throws Exception {
3402+
this.ctx = tf.createSSLContext("TLS", engineProvider);
3403+
SSLEngine c = this.ctx.createSSLEngine("wolfSSL test", 11111);
3404+
c.setUseClientMode(true);
3405+
ByteBuffer[] in = {ByteBuffer.wrap("x".getBytes()), null};
3406+
c.wrap(in, 1, 1, ByteBuffer.allocateDirect(
3407+
c.getSession().getPacketBufferSize()));
3408+
}
3409+
3410+
/* Direct regression: unwrap() readOnly-check must reach
3411+
* out[ofst+length-1]. */
3412+
@Test(expected = java.nio.ReadOnlyBufferException.class)
3413+
public void testUnwrapRejectsReadOnlyAtOffset() throws Exception {
3414+
this.ctx = tf.createSSLContext("TLS", engineProvider);
3415+
SSLEngine s = this.ctx.createSSLEngine();
3416+
s.setUseClientMode(false);
3417+
ByteBuffer[] out = {ByteBuffer.allocate(64),
3418+
ByteBuffer.allocate(64).asReadOnlyBuffer()};
3419+
s.unwrap(ByteBuffer.allocateDirect(
3420+
s.getSession().getPacketBufferSize()), out, 1, 1);
3421+
}
33503422
}
33513423

src/test/com/wolfssl/test/WolfSSLSessionTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,5 +4210,107 @@ public void test_WolfSSLSession_dtlsCidDataExchangeAfterHandshake()
42104210
}
42114211
}
42124212
}
4213+
4214+
/* Regression: read(ByteBuffer) must honor arrayOffset() so a
4215+
* sliced array-backed buffer reads into backing[arrayOffset+pos),
4216+
* not backing[pos). */
4217+
@Test
4218+
public void test_WolfSSLSession_readSlicedByteBuffer() throws Exception {
4219+
final ServerSocket srvSocket = new ServerSocket(0);
4220+
final WolfSSLContext srvCtx = createAndSetupWolfSSLContext(
4221+
srvCert, srvKey, WolfSSL.SSL_FILETYPE_PEM, cliCert,
4222+
WolfSSL.SSLv23_ServerMethod());
4223+
WolfSSLContext cliCtx = createAndSetupWolfSSLContext(
4224+
cliCert, cliKey, WolfSSL.SSL_FILETYPE_PEM, caCert,
4225+
WolfSSL.SSLv23_ClientMethod());
4226+
final byte[] payload = "sliced-buf-payload".getBytes();
4227+
4228+
ExecutorService es = Executors.newSingleThreadExecutor();
4229+
Future<Void> srv = es.submit(() -> {
4230+
try (Socket s = srvSocket.accept()) {
4231+
WolfSSLSession ss = new WolfSSLSession(srvCtx);
4232+
ss.setFd(s);
4233+
int r;
4234+
int e;
4235+
do {
4236+
r = ss.accept();
4237+
e = ss.getError(r);
4238+
} while (r != WolfSSL.SSL_SUCCESS &&
4239+
(e == WolfSSL.SSL_ERROR_WANT_READ ||
4240+
e == WolfSSL.SSL_ERROR_WANT_WRITE));
4241+
ss.write(payload, payload.length, 0);
4242+
ss.shutdownSSL();
4243+
ss.freeSSL();
4244+
}
4245+
return null;
4246+
});
4247+
4248+
Socket cliSock = null;
4249+
WolfSSLSession cliSes = null;
4250+
try {
4251+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
4252+
srvSocket.getLocalPort());
4253+
cliSes = new WolfSSLSession(cliCtx);
4254+
cliSes.setFd(cliSock);
4255+
int r;
4256+
int e;
4257+
do {
4258+
r = cliSes.connect();
4259+
e = cliSes.getError(r);
4260+
} while (r != WolfSSL.SSL_SUCCESS &&
4261+
(e == WolfSSL.SSL_ERROR_WANT_READ ||
4262+
e == WolfSSL.SSL_ERROR_WANT_WRITE));
4263+
4264+
int prefix = 64;
4265+
ByteBuffer parent = ByteBuffer.allocate(256);
4266+
byte[] backing = parent.array();
4267+
byte sentinel = (byte) 0xA5;
4268+
Arrays.fill(backing, sentinel);
4269+
parent.position(prefix);
4270+
ByteBuffer slice = parent.slice();
4271+
assertEquals(prefix, slice.arrayOffset());
4272+
4273+
int total = 0;
4274+
while (total < payload.length) {
4275+
int n = cliSes.read(slice, payload.length - total, 5000);
4276+
if (n > 0) {
4277+
total += n;
4278+
continue;
4279+
}
4280+
int err = cliSes.getError(n);
4281+
if (err == WolfSSL.SSL_ERROR_WANT_READ ||
4282+
err == WolfSSL.SSL_ERROR_WANT_WRITE) {
4283+
continue;
4284+
}
4285+
fail("cliSes.read() failed: ret=" + n + " err=" + err +
4286+
" total=" + total + "/" + payload.length);
4287+
}
4288+
4289+
for (int i = 0; i < prefix; i++) {
4290+
assertEquals("backing[" + i + "] corrupted",
4291+
sentinel, backing[i]);
4292+
}
4293+
assertArrayEquals(payload, Arrays.copyOfRange(backing,
4294+
prefix, prefix + payload.length));
4295+
assertEquals(payload.length, slice.position());
4296+
4297+
cliSes.shutdownSSL();
4298+
} finally {
4299+
try {
4300+
srv.get(10, TimeUnit.SECONDS);
4301+
} finally {
4302+
es.shutdownNow();
4303+
if (cliSes != null) {
4304+
cliSes.freeSSL();
4305+
}
4306+
if (cliSock != null) {
4307+
cliSock.close();
4308+
}
4309+
srvSocket.close();
4310+
cliCtx.free();
4311+
srvCtx.free();
4312+
}
4313+
}
4314+
}
42134315
}
42144316

0 commit comments

Comments
 (0)