-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not signal onComplete when the incoming buffer length is less…
… than the cipher block (#209)
- Loading branch information
Showing
5 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/java/software/amazon/encryption/s3/utils/TinyBufferAsyncRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package software.amazon.encryption.s3.utils; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Optional; | ||
|
||
/** | ||
* AsyncRequestBody which wraps another AsyncRequestBody with a {@link TinyBufferSubscriber}. | ||
* This is useful for testing poor network conditions where buffers may not be larger than | ||
* the cipher's block size. | ||
*/ | ||
public class TinyBufferAsyncRequestBody implements AsyncRequestBody { | ||
|
||
private final AsyncRequestBody wrappedAsyncRequestBody; | ||
|
||
public TinyBufferAsyncRequestBody(final AsyncRequestBody wrappedRequestBody) { | ||
wrappedAsyncRequestBody = wrappedRequestBody; | ||
} | ||
|
||
@Override | ||
public Optional<Long> contentLength() { | ||
return wrappedAsyncRequestBody.contentLength(); | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
wrappedAsyncRequestBody.subscribe(new TinyBufferSubscriber(s)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/software/amazon/encryption/s3/utils/TinyBufferSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package software.amazon.encryption.s3.utils; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.utils.BinaryUtils; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Subscriber which purposefully limits the size of buffers sent to | ||
* the wrapped subscriber. This is useful for simulating adverse network conditions. | ||
*/ | ||
public class TinyBufferSubscriber implements Subscriber<ByteBuffer> { | ||
|
||
private final Subscriber<? super ByteBuffer> wrappedSubscriber; | ||
|
||
public TinyBufferSubscriber(final Subscriber wrappedSubscriber){ | ||
this.wrappedSubscriber = wrappedSubscriber; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
wrappedSubscriber.onSubscribe(s); | ||
} | ||
|
||
@Override | ||
public void onNext(ByteBuffer b) { | ||
int i = 0; | ||
// any value below GCM block size works | ||
int chunkSize = 5; | ||
while (b.remaining() > chunkSize) { | ||
ByteBuffer tb = b.slice(); | ||
tb.limit(chunkSize); | ||
byte[] intermediateBuf = BinaryUtils.copyBytesFrom(tb, chunkSize); | ||
b.position(i + chunkSize); | ||
i += chunkSize; | ||
wrappedSubscriber.onNext(ByteBuffer.wrap(intermediateBuf)); | ||
} | ||
// send the rest of the bytes | ||
ByteBuffer sb = b.slice(); | ||
sb.limit(b.remaining()); | ||
byte[] intermedBuf = BinaryUtils.copyBytesFrom(sb, chunkSize); | ||
wrappedSubscriber.onNext(ByteBuffer.wrap(intermedBuf)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
wrappedSubscriber.onError(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
wrappedSubscriber.onComplete(); | ||
} | ||
} |