Skip to content

Commit d10ee29

Browse files
committed
Add regression tests
1 parent 4ef185d commit d10ee29

2 files changed

Lines changed: 151 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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,5 +4210,84 @@ 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, e;
4234+
do { r = ss.accept(); e = ss.getError(r); }
4235+
while (r != WolfSSL.SSL_SUCCESS &&
4236+
(e == WolfSSL.SSL_ERROR_WANT_READ ||
4237+
e == WolfSSL.SSL_ERROR_WANT_WRITE));
4238+
ss.write(payload, payload.length, 0);
4239+
ss.shutdownSSL();
4240+
ss.freeSSL();
4241+
}
4242+
return null;
4243+
});
4244+
4245+
Socket cliSock = null;
4246+
WolfSSLSession cliSes = null;
4247+
try {
4248+
cliSock = new Socket(InetAddress.getLocalHost(),
4249+
srvSocket.getLocalPort());
4250+
cliSes = new WolfSSLSession(cliCtx);
4251+
cliSes.setFd(cliSock);
4252+
int r, e;
4253+
do { r = cliSes.connect(); e = cliSes.getError(r); }
4254+
while (r != WolfSSL.SSL_SUCCESS &&
4255+
(e == WolfSSL.SSL_ERROR_WANT_READ ||
4256+
e == WolfSSL.SSL_ERROR_WANT_WRITE));
4257+
4258+
int prefix = 64;
4259+
ByteBuffer parent = ByteBuffer.allocate(256);
4260+
byte[] backing = parent.array();
4261+
byte sentinel = (byte) 0xA5;
4262+
Arrays.fill(backing, sentinel);
4263+
parent.position(prefix);
4264+
ByteBuffer slice = parent.slice();
4265+
assertEquals(prefix, slice.arrayOffset());
4266+
4267+
int total = 0;
4268+
while (total < payload.length) {
4269+
int n = cliSes.read(slice, payload.length - total, 5000);
4270+
if (n > 0) total += n;
4271+
}
4272+
4273+
for (int i = 0; i < prefix; i++) {
4274+
assertEquals("backing[" + i + "] corrupted",
4275+
sentinel, backing[i]);
4276+
}
4277+
assertArrayEquals(payload, Arrays.copyOfRange(backing,
4278+
prefix, prefix + payload.length));
4279+
assertEquals(payload.length, slice.position());
4280+
4281+
cliSes.shutdownSSL();
4282+
} finally {
4283+
srv.get(10, TimeUnit.SECONDS);
4284+
es.shutdown();
4285+
if (cliSes != null) cliSes.freeSSL();
4286+
if (cliSock != null) cliSock.close();
4287+
srvSocket.close();
4288+
cliCtx.free();
4289+
srvCtx.free();
4290+
}
4291+
}
42134292
}
42144293

0 commit comments

Comments
 (0)