|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.sync; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; |
| 30 | +import software.amazon.awssdk.checksums.SdkChecksum; |
| 31 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 32 | +import software.amazon.awssdk.utils.BinaryUtils; |
| 33 | +import software.amazon.awssdk.utils.IoUtils; |
| 34 | + |
| 35 | +class BufferingContentStreamProviderTest { |
| 36 | + private static final SdkChecksum CRC32 = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32); |
| 37 | + private static final byte[] TEST_DATA = "BufferingContentStreamProviderTest".getBytes(StandardCharsets.UTF_8); |
| 38 | + private static final String TEST_DATA_CHECKSUM = "f9ed1825"; |
| 39 | + |
| 40 | + private RequestBody requestBody; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setup() { |
| 44 | + ByteArrayInputStream stream = new ByteArrayInputStream(TEST_DATA); |
| 45 | + requestBody = RequestBody.fromContentProvider(() -> stream, "text/plain"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void newStream_alwaysStartsAtBeginning() { |
| 50 | + String stream1Crc32 = getCrc32(requestBody.contentStreamProvider().newStream()); |
| 51 | + String stream2Crc32 = getCrc32(requestBody.contentStreamProvider().newStream()); |
| 52 | + |
| 53 | + assertThat(stream1Crc32).isEqualTo(TEST_DATA_CHECKSUM); |
| 54 | + assertThat(stream2Crc32).isEqualTo(TEST_DATA_CHECKSUM); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void newStream_buffersSkippedBytes() throws IOException { |
| 59 | + InputStream stream1 = requestBody.contentStreamProvider().newStream(); |
| 60 | + |
| 61 | + assertThat(stream1.skip(Long.MAX_VALUE)).isEqualTo(TEST_DATA.length); |
| 62 | + |
| 63 | + String stream2Crc32 = getCrc32(requestBody.contentStreamProvider().newStream()); |
| 64 | + |
| 65 | + assertThat(stream2Crc32).isEqualTo(TEST_DATA_CHECKSUM); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void newStream_oneByteReads_dataBufferedCorrectly() throws IOException { |
| 70 | + InputStream stream = requestBody.contentStreamProvider().newStream(); |
| 71 | + int read; |
| 72 | + do { |
| 73 | + read = stream.read(); |
| 74 | + } while (read != -1); |
| 75 | + |
| 76 | + assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(TEST_DATA_CHECKSUM); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void newStream_wholeArrayReads_dataBufferedCorrectly() throws IOException { |
| 81 | + InputStream stream = requestBody.contentStreamProvider().newStream(); |
| 82 | + int read; |
| 83 | + byte[] buff = new byte[32]; |
| 84 | + do { |
| 85 | + read = stream.read(buff); |
| 86 | + } while (read != -1); |
| 87 | + |
| 88 | + assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(TEST_DATA_CHECKSUM); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void newStream_offsetArrayReads_dataBufferedCorrectly() throws IOException { |
| 93 | + InputStream stream = requestBody.contentStreamProvider().newStream(); |
| 94 | + int read; |
| 95 | + byte[] buff = new byte[32]; |
| 96 | + do { |
| 97 | + read = stream.read(buff, 0, 32); |
| 98 | + } while (read != -1); |
| 99 | + |
| 100 | + assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(TEST_DATA_CHECKSUM); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void newStream_closeClosesDelegateStream() throws IOException { |
| 105 | + InputStream stream = Mockito.spy(new ByteArrayInputStream(TEST_DATA)); |
| 106 | + requestBody = RequestBody.fromContentProvider(() -> stream, "text/plain"); |
| 107 | + requestBody.contentStreamProvider().newStream().close(); |
| 108 | + |
| 109 | + Mockito.verify(stream).close(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void newStream_allDataBuffered_closesDelegateStream() throws IOException { |
| 114 | + InputStream delegateStream = Mockito.spy(new ByteArrayInputStream(TEST_DATA)); |
| 115 | + |
| 116 | + requestBody = RequestBody.fromContentProvider(() -> delegateStream, "text/plain"); |
| 117 | + |
| 118 | + IoUtils.drainInputStream(requestBody.contentStreamProvider().newStream()); |
| 119 | + Mockito.verify(delegateStream, Mockito.atLeast(1)).read(any(), anyInt(), anyInt()); |
| 120 | + Mockito.verify(delegateStream).close(); |
| 121 | + |
| 122 | + IoUtils.drainInputStream(requestBody.contentStreamProvider().newStream()); |
| 123 | + Mockito.verifyNoMoreInteractions(delegateStream); |
| 124 | + } |
| 125 | + |
| 126 | + private static String getCrc32(InputStream inputStream) { |
| 127 | + byte[] buff = new byte[1024]; |
| 128 | + int read; |
| 129 | + |
| 130 | + CRC32.reset(); |
| 131 | + try { |
| 132 | + while ((read = inputStream.read(buff)) != -1) { |
| 133 | + CRC32.update(buff, 0, read); |
| 134 | + } |
| 135 | + } catch (IOException e) { |
| 136 | + throw new RuntimeException(e); |
| 137 | + } |
| 138 | + |
| 139 | + return BinaryUtils.toHex(CRC32.getChecksumBytes()); |
| 140 | + } |
| 141 | +} |
0 commit comments