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