-
Notifications
You must be signed in to change notification settings - Fork 912
ChecksumValidatingPublisher deals with any packetization #966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bbb907
Fix ChecksumValidatingPublisher to deal with any packetization of the…
29bea2c
Merge branch 'master' into aws_965
spfink 7087f7a
Merge branch 'master' into aws_965
spfink fa60fac
Fixed copyright message
fbb89c1
Merge branch 'master' into aws_965
9ca5bd5
Merge branch 'master' into aws_965
bc27546
Merge branch 'master' into aws_965
zoewangg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"type": "bugfix", | ||
"description": "ChecksumValidatingPublisher deals with any packetization of the incoming data. See https://github.com/aws/aws-sdk-java-v2/issues/965" | ||
} |
This file contains hidden or 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
187 changes: 187 additions & 0 deletions
187
...st/java/software/amazon/awssdk/services/s3/checksums/ChecksumValidatingPublisherTest.java
This file contains hidden or 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,187 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package software.amazon.awssdk.services.s3.checksums; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
import software.amazon.awssdk.core.checksums.Md5Checksum; | ||
|
||
/** | ||
* Unit test for ChecksumValidatingPublisher | ||
*/ | ||
public class ChecksumValidatingPublisherTest { | ||
private static int TEST_DATA_SIZE = 32; // size of the test data, in bytes | ||
private static final int CHECKSUM_SIZE = 16; | ||
private static byte[] testData; | ||
|
||
@BeforeClass | ||
public static void populateData() { | ||
testData = new byte[TEST_DATA_SIZE + CHECKSUM_SIZE]; | ||
for (int i = 0; i < TEST_DATA_SIZE; i++) { | ||
testData[i] = (byte)(i & 0x7f); | ||
} | ||
final Md5Checksum checksum = new Md5Checksum(); | ||
checksum.update(testData, 0, TEST_DATA_SIZE); | ||
byte[] checksumBytes = checksum.getChecksumBytes(); | ||
for (int i = 0; i < CHECKSUM_SIZE; i++) { | ||
testData[TEST_DATA_SIZE + i] = checksumBytes[i]; | ||
} | ||
} | ||
|
||
@Test | ||
public void testSinglePacket() { | ||
final TestPublisher driver = new TestPublisher(); | ||
final TestSubscriber s = new TestSubscriber(Arrays.copyOfRange(testData, 0, TEST_DATA_SIZE)); | ||
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher(driver, new Md5Checksum(), TEST_DATA_SIZE + CHECKSUM_SIZE); | ||
p.subscribe(s); | ||
|
||
driver.doOnNext(ByteBuffer.wrap(testData)); | ||
driver.doOnComplete(); | ||
|
||
assertTrue(s.hasCompleted()); | ||
} | ||
|
||
@Test | ||
public void testTwoPackets() { | ||
for (int i = 1; i < TEST_DATA_SIZE + CHECKSUM_SIZE - 1; i++) { | ||
final TestPublisher driver = new TestPublisher(); | ||
final TestSubscriber s = new TestSubscriber(Arrays.copyOfRange(testData, 0, TEST_DATA_SIZE)); | ||
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher(driver, new Md5Checksum(), TEST_DATA_SIZE + CHECKSUM_SIZE); | ||
p.subscribe(s); | ||
|
||
driver.doOnNext(ByteBuffer.wrap(testData, 0, i)); | ||
driver.doOnNext(ByteBuffer.wrap(testData, i, TEST_DATA_SIZE + CHECKSUM_SIZE - i)); | ||
driver.doOnComplete(); | ||
|
||
assertTrue(s.hasCompleted()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTinyPackets() { | ||
for (int packetSize = 1; packetSize < CHECKSUM_SIZE; packetSize++) { | ||
final TestPublisher driver = new TestPublisher(); | ||
final TestSubscriber s = new TestSubscriber(Arrays.copyOfRange(testData, 0, TEST_DATA_SIZE)); | ||
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher(driver, new Md5Checksum(), TEST_DATA_SIZE + CHECKSUM_SIZE); | ||
p.subscribe(s); | ||
int currOffset = 0; | ||
while (currOffset < TEST_DATA_SIZE + CHECKSUM_SIZE) { | ||
final int toSend = Math.min(packetSize, TEST_DATA_SIZE + CHECKSUM_SIZE - currOffset); | ||
driver.doOnNext(ByteBuffer.wrap(testData, currOffset, toSend)); | ||
currOffset += toSend; | ||
} | ||
driver.doOnComplete(); | ||
|
||
assertTrue(s.hasCompleted()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testUnknownLength() { | ||
// When the length is unknown, the last 16 bytes are treated as a checksum, but are later ignored when completing | ||
final TestPublisher driver = new TestPublisher(); | ||
final TestSubscriber s = new TestSubscriber(Arrays.copyOfRange(testData, 0, TEST_DATA_SIZE)); | ||
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher(driver, new Md5Checksum(), 0); | ||
p.subscribe(s); | ||
|
||
byte[] randomChecksumData = new byte[testData.length]; | ||
System.arraycopy(testData, 0, randomChecksumData, 0, TEST_DATA_SIZE); | ||
for (int i = TEST_DATA_SIZE; i < randomChecksumData.length; i++) { | ||
randomChecksumData[i] = (byte)((testData[i] + 1) & 0x7f); | ||
} | ||
|
||
driver.doOnNext(ByteBuffer.wrap(randomChecksumData)); | ||
driver.doOnComplete(); | ||
|
||
assertTrue(s.hasCompleted()); | ||
} | ||
|
||
private class TestSubscriber implements Subscriber<ByteBuffer> { | ||
final byte[] expected; | ||
final List<ByteBuffer> received; | ||
boolean completed; | ||
|
||
TestSubscriber(byte[] expected) { | ||
this.expected = expected; | ||
this.received = new ArrayList<>(); | ||
this.completed = false; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
fail("This method not expected to be invoked"); | ||
throw new UnsupportedOperationException("!!!TODO: implement this"); | ||
} | ||
|
||
@Override | ||
public void onNext(ByteBuffer buffer) { | ||
received.add(buffer); | ||
} | ||
|
||
|
||
@Override | ||
public void onError(Throwable t) { | ||
fail("Test failed"); | ||
} | ||
|
||
|
||
@Override | ||
public void onComplete() { | ||
int matchPos = 0; | ||
for (ByteBuffer buffer : received) { | ||
byte[] bufferData = new byte[buffer.limit() - buffer.position()]; | ||
buffer.get(bufferData); | ||
assertArrayEquals(Arrays.copyOfRange(expected, matchPos, matchPos + bufferData.length), bufferData); | ||
matchPos += bufferData.length; | ||
} | ||
assertEquals(expected.length, matchPos); | ||
completed = true; | ||
} | ||
|
||
public boolean hasCompleted() { | ||
return completed; | ||
} | ||
} | ||
|
||
private class TestPublisher implements Publisher<ByteBuffer> { | ||
Subscriber<? super ByteBuffer> s; | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
this.s = s; | ||
} | ||
|
||
public void doOnNext(ByteBuffer b) { | ||
s.onNext(b); | ||
} | ||
|
||
public void doOnComplete() { | ||
s.onComplete(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same copyright profile that we are using. Could you update it?
Here's the intelliJ copyright file template link, or you can copy paste from other files in this repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Zoe,
I'm not affiliated with Amazon, so I thought putting an Amazon copyright there would not be kosher. Am I wrong? I don't mind doing it -- just seems odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws-java-sdk-v2
is under the Apache License 2.0 and we wanted to make sure all the files follow the same style.Thank you for updating it!