Skip to content

Commit a555ca6

Browse files
committed
tweak onComplete logic, fix bug in unit tests
1 parent ee17279 commit a555ca6

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/main/java/software/amazon/encryption/s3/internal/CipherSubscriber.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public void onError(Throwable t) {
129129
public void onComplete() {
130130
// In rare cases, e.g. when the last part of a low-level MPU has 0 length,
131131
// onComplete will be called before onNext is called once.
132-
// tagLength should only be added on Encrypt
133-
if (contentRead.get() + (isEncrypt ? tagLength : 0) >= contentLength) {
134-
finalBytes();
135-
}
132+
// So, call finalBytes here just in case there's any unsent data left.
133+
// Most likely, finalBytes has already been called by the last onNext,
134+
// but finalBytes guards against multiple invocations so it's safe to call again.
135+
finalBytes();
136136
wrappedSubscriber.onComplete();
137137
}
138138

src/test/java/software/amazon/encryption/s3/internal/CipherSubscriberTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void testSubscriberBehaviorOneChunk() {
185185
// we reject 0-ized IVs, so just do something
186186
iv[0] = 1;
187187
SimpleSubscriber wrappedSubscriber = new SimpleSubscriber();
188-
CipherSubscriber subscriber = new CipherSubscriber(wrappedSubscriber, (long) plaintext.getBytes(StandardCharsets.UTF_8).length, materials, iv);
188+
CipherSubscriber subscriber = new CipherSubscriber(wrappedSubscriber, materials.getCiphertextLength(), materials, iv);
189189

190190
// Act
191191
TestPublisher<ByteBuffer> publisher = new TestPublisher<>();
@@ -239,7 +239,7 @@ public void testSubscriberBehaviorTagLengthLastChunk() {
239239
// we reject 0-ized IVs, so just do something non-zero
240240
iv[0] = 1;
241241
SimpleSubscriber wrappedSubscriber = new SimpleSubscriber();
242-
CipherSubscriber subscriber = new CipherSubscriber(wrappedSubscriber, (long) plaintext.getBytes(StandardCharsets.UTF_8).length, materials, iv);
242+
CipherSubscriber subscriber = new CipherSubscriber(wrappedSubscriber, materials.getCiphertextLength(), materials, iv);
243243

244244
// Setup Publisher
245245
TestPublisher<ByteBuffer> publisher = new TestPublisher<>();

0 commit comments

Comments
 (0)