@@ -993,6 +993,92 @@ public void wrapPreconditions() throws Exception {
993993 newConnectedEngine ().wrap (buffers , arrayLength , 0 , buffer );
994994 }
995995
996+ @ Test
997+ public void unwrapPreconditions () throws Exception {
998+ int bufferSize = 128 ;
999+ int arrayLength = 5 ;
1000+ ByteBuffer buffer = ByteBuffer .allocate (bufferSize );
1001+ ByteBuffer readOnlyBuffer = buffer .asReadOnlyBuffer ();
1002+ ByteBuffer [] buffers = BufferType .HEAP .newBufferArray (arrayLength , bufferSize );
1003+ ByteBuffer [] buffersWithNullEntry = Arrays .copyOf (buffers , buffers .length );
1004+ int nullBufferIndex = 2 ;
1005+ buffersWithNullEntry [nullBufferIndex ] = null ;
1006+ ByteBuffer [] buffersWithReadOnlyEntry = Arrays .copyOf (buffers , buffers .length );
1007+ int readOnlyBufferIndex = 2 ;
1008+ buffersWithReadOnlyEntry [readOnlyBufferIndex ] =
1009+ buffersWithReadOnlyEntry [readOnlyBufferIndex ].asReadOnlyBuffer ();
1010+
1011+ // Client/server mode not set => IllegalStateException
1012+ assertThrows (
1013+ IllegalStateException .class , () -> newUnconnectedEngine ().unwrap (buffer , buffer ));
1014+ assertThrows (
1015+ IllegalStateException .class , () -> newUnconnectedEngine ().unwrap (buffer , buffers ));
1016+ assertThrows (IllegalStateException .class ,
1017+ () -> newUnconnectedEngine ().unwrap (buffer , buffers , 0 , 1 ));
1018+
1019+ // Read-only destination => ReadOnlyBufferException
1020+ assertThrows (ReadOnlyBufferException .class ,
1021+ () -> newConnectedEngine ().unwrap (buffer , readOnlyBuffer ));
1022+ assertThrows (ReadOnlyBufferException .class ,
1023+ () -> newConnectedEngine ().unwrap (buffer , buffersWithReadOnlyEntry ));
1024+ assertThrows (ReadOnlyBufferException .class ,
1025+ ()
1026+ -> newConnectedEngine ().unwrap (
1027+ buffer , buffersWithReadOnlyEntry , 0 , arrayLength ));
1028+
1029+ // Null destination => IllegalArgumentException
1030+ assertThrows (IllegalArgumentException .class ,
1031+ () -> newConnectedEngine ().unwrap (buffer , (ByteBuffer ) null ));
1032+ assertThrows (IllegalArgumentException .class ,
1033+ () -> newConnectedEngine ().unwrap (buffer , (ByteBuffer []) null ));
1034+ assertThrows (IllegalArgumentException .class ,
1035+ () -> newConnectedEngine ().unwrap (buffer , null , 0 , 1 ));
1036+
1037+ // Null source => IllegalArgumentException
1038+ assertThrows (
1039+ IllegalArgumentException .class , () -> newConnectedEngine ().unwrap (null , buffer ));
1040+ assertThrows (
1041+ IllegalArgumentException .class , () -> newConnectedEngine ().unwrap (null , buffers ));
1042+ assertThrows (IllegalArgumentException .class ,
1043+ () -> newConnectedEngine ().unwrap (null , buffers , 0 , 1 ));
1044+
1045+ // Null entries in buffer array => IllegalArgumentException
1046+ assertThrows (IllegalArgumentException .class ,
1047+ () -> newConnectedEngine ().unwrap (buffer , buffersWithNullEntry ));
1048+ assertThrows (IllegalArgumentException .class ,
1049+ () -> newConnectedEngine ().unwrap (buffer , buffersWithNullEntry , 0 , arrayLength ));
1050+
1051+ // Bad offset or length => IndexOutOfBoundsException
1052+ assertThrows (IndexOutOfBoundsException .class ,
1053+ () -> newConnectedEngine ().unwrap (buffer , buffers , 0 , arrayLength + 1 ));
1054+ assertThrows (IndexOutOfBoundsException .class ,
1055+ () -> newConnectedEngine ().unwrap (buffer , buffers , arrayLength , 1 ));
1056+ wrapThenUnwrap (bufferSize , buffers , 0 , arrayLength );
1057+ // Zero length array is allowed
1058+ wrapThenUnwrap (bufferSize , buffers , 0 , 0 );
1059+ wrapThenUnwrap (bufferSize , buffers , arrayLength , 0 );
1060+ }
1061+
1062+ private void wrapThenUnwrap (int bufferSize , ByteBuffer [] dest , int offset , int length )
1063+ throws Exception {
1064+ ByteBuffer src = ByteBuffer .allocate (bufferSize );
1065+ ByteBuffer tlsBuffer = ByteBuffer .allocate (bufferSize + 128 );
1066+ tlsBuffer .clear ();
1067+
1068+ try (TestSSLEnginePair pair = TestSSLEnginePair .create ()) {
1069+ SSLEngineResult result = pair .client .wrap (src , tlsBuffer );
1070+ assertEquals (Status .OK , result .getStatus ());
1071+
1072+ tlsBuffer .flip ();
1073+ for (int i = offset ; i < offset + length ; i ++) {
1074+ dest [i ].clear ();
1075+ }
1076+ // Unwrap result ignored because unwrap may not succeed (e.g. overflowing zero length
1077+ // buffer array), but it should not throw any exception due to preconditions
1078+ pair .server .unwrap (tlsBuffer , dest , offset , length );
1079+ }
1080+ }
1081+
9961082 @ Test
9971083 public void bufferArrayOffsets () throws Exception {
9981084 TestSSLEnginePair pair = TestSSLEnginePair .create ();
0 commit comments