@@ -3420,5 +3420,126 @@ public void testUnwrapRejectsReadOnlyAtOffset() throws Exception {
34203420 s .unwrap (ByteBuffer .allocateDirect (
34213421 s .getSession ().getPacketBufferSize ()), out , 1 , 1 );
34223422 }
3423+
3424+ /* Verify unwrap() with ofst > 0 placed all `data` bytes into
3425+ * out[2] and out[3], and that bytesProduced matches. Regression
3426+ * for the `idx + ofst >= length` copy-loop bound in unwrap. */
3427+ private void assertOffsetUnwrapOk (SSLEngineResult result ,
3428+ ByteBuffer [] outArr , byte [] data ) {
3429+
3430+ assertEquals (data .length , result .bytesProduced ());
3431+ assertEquals (0 , outArr [0 ].position ());
3432+ assertEquals (0 , outArr [1 ].position ());
3433+
3434+ outArr [2 ].flip ();
3435+ outArr [3 ].flip ();
3436+ int n2 = outArr [2 ].remaining ();
3437+ int n3 = outArr [3 ].remaining ();
3438+ assertEquals (data .length , n2 + n3 );
3439+
3440+ byte [] got = new byte [n2 + n3 ];
3441+ outArr [2 ].get (got , 0 , n2 );
3442+ outArr [3 ].get (got , n2 , n3 );
3443+ assertTrue (java .util .Arrays .equals (got , data ));
3444+ }
3445+
3446+ /* Wrap `data` from client and return server's network buffer
3447+ * positioned for unwrap(). */
3448+ private ByteBuffer wrapForServer (SSLEngine client , byte [] data )
3449+ throws SSLException {
3450+
3451+ ByteBuffer netBuf = ByteBuffer .allocateDirect (
3452+ client .getSession ().getPacketBufferSize ());
3453+ SSLEngineResult r = client .wrap (ByteBuffer .wrap (data ), netBuf );
3454+ assertEquals (SSLEngineResult .Status .OK , r .getStatus ());
3455+ netBuf .flip ();
3456+ return netBuf ;
3457+ }
3458+
3459+ @ Test
3460+ public void testUnwrapOffsetMultipleBuffers () throws Exception {
3461+
3462+ this .ctx = tf .createSSLContext ("TLS" , engineProvider );
3463+ SSLEngine server = this .ctx .createSSLEngine ();
3464+ SSLEngine client = this .ctx .createSSLEngine ("wolfSSL test" , 11111 );
3465+ server .setUseClientMode (false );
3466+ client .setUseClientMode (true );
3467+ assertEquals (0 , tf .testConnection (server , client , null , null , "x" ));
3468+
3469+ byte [] data = new byte [1024 ];
3470+ new Random ().nextBytes (data );
3471+ ByteBuffer netBuf = wrapForServer (client , data );
3472+
3473+ ByteBuffer [] outArr = new ByteBuffer [] {
3474+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 ),
3475+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 )
3476+ };
3477+ SSLEngineResult r = server .unwrap (netBuf , outArr , 2 , 2 );
3478+ assertEquals (SSLEngineResult .Status .OK , r .getStatus ());
3479+ assertOffsetUnwrapOk (r , outArr , data );
3480+ }
3481+
3482+ @ Test
3483+ public void testUnwrapPendingAppDataWithOffset () throws Exception {
3484+
3485+ this .ctx = tf .createSSLContext ("TLS" , engineProvider );
3486+ SSLEngine server = this .ctx .createSSLEngine ();
3487+ SSLEngine client = this .ctx .createSSLEngine ("wolfSSL test" , 11111 );
3488+ server .setUseClientMode (false );
3489+ client .setUseClientMode (true );
3490+ assertEquals (0 , tf .testConnection (server , client , null , null , "x" ));
3491+
3492+ byte [] data = new byte [1024 ];
3493+ new Random ().nextBytes (data );
3494+ ByteBuffer netBuf = wrapForServer (client , data );
3495+
3496+ /* First unwrap into too-small output stashes pendingAppData */
3497+ SSLEngineResult r = server .unwrap (netBuf , ByteBuffer .allocate (64 ));
3498+ assertEquals (SSLEngineResult .Status .BUFFER_OVERFLOW , r .getStatus ());
3499+
3500+ ByteBuffer [] outArr = new ByteBuffer [] {
3501+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 ),
3502+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 )
3503+ };
3504+ r = server .unwrap (netBuf , outArr , 2 , 2 );
3505+ assertOffsetUnwrapOk (r , outArr , data );
3506+ }
3507+
3508+ @ Test
3509+ public void testUnwrapPendingAppDataReStashWithOffset ()
3510+ throws Exception {
3511+
3512+ this .ctx = tf .createSSLContext ("TLS" , engineProvider );
3513+ SSLEngine server = this .ctx .createSSLEngine ();
3514+ SSLEngine client = this .ctx .createSSLEngine ("wolfSSL test" , 11111 );
3515+ server .setUseClientMode (false );
3516+ client .setUseClientMode (true );
3517+ assertEquals (0 , tf .testConnection (server , client , null , null , "x" ));
3518+
3519+ byte [] data = new byte [1024 ];
3520+ new Random ().nextBytes (data );
3521+ ByteBuffer netBuf = wrapForServer (client , data );
3522+
3523+ /* First unwrap stashes pendingAppData */
3524+ SSLEngineResult r = server .unwrap (netBuf , ByteBuffer .allocate (64 ));
3525+ assertEquals (SSLEngineResult .Status .BUFFER_OVERFLOW , r .getStatus ());
3526+
3527+ /* Second unwrap: ofst > 0 but total still too small;
3528+ * pendingAppData must survive intact for a later call */
3529+ ByteBuffer [] tooSmall = new ByteBuffer [] {
3530+ ByteBuffer .allocate (200 ), ByteBuffer .allocate (200 ),
3531+ ByteBuffer .allocate (200 ), ByteBuffer .allocate (200 )
3532+ };
3533+ r = server .unwrap (netBuf , tooSmall , 2 , 2 );
3534+ assertEquals (SSLEngineResult .Status .BUFFER_OVERFLOW , r .getStatus ());
3535+
3536+ /* Third unwrap: ofst > 0 with room drains the stash */
3537+ ByteBuffer [] outArr = new ByteBuffer [] {
3538+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 ),
3539+ ByteBuffer .allocate (600 ), ByteBuffer .allocate (600 )
3540+ };
3541+ r = server .unwrap (netBuf , outArr , 2 , 2 );
3542+ assertOffsetUnwrapOk (r , outArr , data );
3543+ }
34233544}
34243545
0 commit comments