@@ -1059,7 +1059,93 @@ private void assertWrapSucceeds(ByteBuffer[] buffers, int offset, int length) th
10591059 }
10601060
10611061 @ Test
1062- public void bufferArrayOffsets () throws Exception {
1062+ public void unwrapPreconditions () throws Exception {
1063+ int bufferSize = 128 ;
1064+ int arrayLength = 5 ;
1065+ ByteBuffer buffer = ByteBuffer .allocate (bufferSize );
1066+ ByteBuffer readOnlyBuffer = buffer .asReadOnlyBuffer ();
1067+ ByteBuffer [] buffers = BufferType .HEAP .newBufferArray (arrayLength , bufferSize );
1068+ ByteBuffer [] buffersWithNullEntry = Arrays .copyOf (buffers , buffers .length );
1069+ int nullBufferIndex = 2 ;
1070+ buffersWithNullEntry [nullBufferIndex ] = null ;
1071+ ByteBuffer [] buffersWithReadOnlyEntry = Arrays .copyOf (buffers , buffers .length );
1072+ int readOnlyBufferIndex = 2 ;
1073+ buffersWithReadOnlyEntry [readOnlyBufferIndex ] =
1074+ buffersWithReadOnlyEntry [readOnlyBufferIndex ].asReadOnlyBuffer ();
1075+
1076+ // Client/server mode not set => IllegalStateException
1077+ assertThrows (
1078+ IllegalStateException .class , () -> newUnconnectedEngine ().unwrap (buffer , buffer ));
1079+ assertThrows (
1080+ IllegalStateException .class , () -> newUnconnectedEngine ().unwrap (buffer , buffers ));
1081+ assertThrows (IllegalStateException .class ,
1082+ () -> newUnconnectedEngine ().unwrap (buffer , buffers , 0 , 1 ));
1083+
1084+ // Read-only destination => ReadOnlyBufferException
1085+ assertThrows (ReadOnlyBufferException .class ,
1086+ () -> newConnectedEngine ().unwrap (buffer , readOnlyBuffer ));
1087+ assertThrows (ReadOnlyBufferException .class ,
1088+ () -> newConnectedEngine ().unwrap (buffer , buffersWithReadOnlyEntry ));
1089+ assertThrows (ReadOnlyBufferException .class ,
1090+ ()
1091+ -> newConnectedEngine ().unwrap (
1092+ buffer , buffersWithReadOnlyEntry , 0 , arrayLength ));
1093+
1094+ // Null destination => IllegalArgumentException
1095+ assertThrows (IllegalArgumentException .class ,
1096+ () -> newConnectedEngine ().unwrap (buffer , (ByteBuffer ) null ));
1097+ assertThrows (IllegalArgumentException .class ,
1098+ () -> newConnectedEngine ().unwrap (buffer , (ByteBuffer []) null ));
1099+ assertThrows (IllegalArgumentException .class ,
1100+ () -> newConnectedEngine ().unwrap (buffer , null , 0 , 1 ));
1101+
1102+ // Null source => IllegalArgumentException
1103+ assertThrows (
1104+ IllegalArgumentException .class , () -> newConnectedEngine ().unwrap (null , buffer ));
1105+ assertThrows (
1106+ IllegalArgumentException .class , () -> newConnectedEngine ().unwrap (null , buffers ));
1107+ assertThrows (IllegalArgumentException .class ,
1108+ () -> newConnectedEngine ().unwrap (null , buffers , 0 , 1 ));
1109+
1110+ // Null entries in buffer array => IllegalArgumentException
1111+ assertThrows (IllegalArgumentException .class ,
1112+ () -> newConnectedEngine ().unwrap (buffer , buffersWithNullEntry ));
1113+ assertThrows (IllegalArgumentException .class ,
1114+ () -> newConnectedEngine ().unwrap (buffer , buffersWithNullEntry , 0 , arrayLength ));
1115+
1116+ // Bad offset or length => IndexOutOfBoundsException
1117+ assertThrows (IndexOutOfBoundsException .class ,
1118+ () -> newConnectedEngine ().unwrap (buffer , buffers , 0 , arrayLength + 1 ));
1119+ assertThrows (IndexOutOfBoundsException .class ,
1120+ () -> newConnectedEngine ().unwrap (buffer , buffers , arrayLength , 1 ));
1121+ wrapThenUnwrap (bufferSize , buffers , 0 , arrayLength );
1122+ // Zero length array is allowed
1123+ wrapThenUnwrap (bufferSize , buffers , 0 , 0 );
1124+ wrapThenUnwrap (bufferSize , buffers , arrayLength , 0 );
1125+ }
1126+
1127+ private void wrapThenUnwrap (int bufferSize , ByteBuffer [] dest , int offset , int length )
1128+ throws Exception {
1129+ ByteBuffer src = ByteBuffer .allocate (bufferSize );
1130+ ByteBuffer tlsBuffer = ByteBuffer .allocate (bufferSize + 128 );
1131+ tlsBuffer .clear ();
1132+
1133+ try (TestSSLEnginePair pair = TestSSLEnginePair .create ()) {
1134+ SSLEngineResult result = pair .client .wrap (src , tlsBuffer );
1135+ assertEquals (Status .OK , result .getStatus ());
1136+
1137+ tlsBuffer .flip ();
1138+ for (int i = offset ; i < offset + length ; i ++) {
1139+ dest [i ].clear ();
1140+ }
1141+ // Unwrap result ignored because unwrap may not succeed (e.g. overflowing zero length
1142+ // buffer array), but it should not throw any exception due to preconditions
1143+ pair .server .unwrap (tlsBuffer , dest , offset , length );
1144+ }
1145+ }
1146+
1147+ @ Test
1148+ public void bufferArrayOffsets () throws Exception {
10631149 TestSSLEnginePair pair = TestSSLEnginePair .create ();
10641150 ByteBuffer tlsBuffer = ByteBuffer .allocate (600 );
10651151 int bufferSize = 100 ;
0 commit comments