Skip to content
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

Stream size validation update #18624

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,48 @@ class HelperTest extends APISpec {
.verifyComplete()
}

/*
This test covers the switch from using available() to using read() to check that a stream is done when converting
from a stream to a flux. We previously used to assert that, when we had read length bytes from the stream that
available() == 0, but available only returns an estimate and is not reliable. Now we assert that read() == -1
*/
def "Utility convertStreamToBuffer available"() {
setup:
def data = getRandomByteArray(10)

when:
def flux = Utility.convertStreamToByteBuffer(new testBAIS(data), 10, 10, true)

then: "When the stream is the right length but available always returns > 0, do not throw"
StepVerifier.create(flux)
.assertNext(){buffer -> assert buffer.compareTo(ByteBuffer.wrap(data)) == 0 }
.verifyComplete()
// subscribe multiple times and ensure data is same each time
StepVerifier.create(flux)
.assertNext(){buffer -> assert buffer.compareTo(ByteBuffer.wrap(data)) == 0 }
.verifyComplete()

when: "When the stream is actually longer than the length, throw"
flux = Utility.convertStreamToByteBuffer(new testBAIS(data), 9, 10, true)

then:
StepVerifier.create(flux)
.assertNext(){buffer -> assert buffer.compareTo(ByteBuffer.wrap(data, 0, 9)) == 0}
.verifyError(IllegalStateException.class)
}

class testBAIS extends ByteArrayInputStream {

testBAIS(byte[] data) {
super(data)
}

@Override
public synchronized int available() {
return 10
}
}

def "PageList custom deserializer"() {
setup:
def responseXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://REDACTED.blob.core.windows.net/jtcutilityconvertstreamtobufferavailable069264aaa25c09?restype=container",
"Headers" : {
"x-ms-version" : "2020-04-08",
"User-Agent" : "azsdk-java-azure-storage-blob/12.10.0-beta.2 (11.0.6; Windows 10; 10.0)",
"x-ms-client-request-id" : "83ed2781-8dc3-410a-bb4a-c1585ae0bfc6"
},
"Response" : {
"x-ms-version" : "2020-04-08",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"ETag" : "0x8D8B8E0DC694EC9",
"Last-Modified" : "Thu, 14 Jan 2021 23:05:18 GMT",
"retry-after" : "0",
"Content-Length" : "0",
"StatusCode" : "201",
"x-ms-request-id" : "4f6145dc-701e-0041-41c9-eaaec5000000",
"Date" : "Thu, 14 Jan 2021 23:05:17 GMT",
"x-ms-client-request-id" : "83ed2781-8dc3-410a-bb4a-c1585ae0bfc6"
},
"Exception" : null
}, {
"Method" : "GET",
"Uri" : "https://REDACTED.blob.core.windows.net?prefix=jtcutilityconvertstreamtobufferavailable&comp=list",
"Headers" : {
"x-ms-version" : "2020-04-08",
"User-Agent" : "azsdk-java-azure-storage-blob/12.10.0-beta.2 (11.0.6; Windows 10; 10.0)",
"x-ms-client-request-id" : "3291daa7-4a00-4174-9eb8-76902a002798"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"Access-Control-Expose-Headers" : "x-ms-client-request-id",
"x-ms-version" : "2020-04-08",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"Access-Control-Allow-Origin" : "*",
"retry-after" : "0",
"StatusCode" : "200",
"x-ms-request-id" : "4f61461d-701e-0041-7ac9-eaaec5000000",
"Body" : "<?xml version=\"1.0\" encoding=\"utf-8\"?><EnumerationResults ServiceEndpoint=\"https://seansoftdeletecanary.blob.core.windows.net/\"><Prefix>jtcutilityconvertstreamtobufferavailable</Prefix><Containers><Container><Name>jtcutilityconvertstreamtobufferavailable069264aaa25c09</Name><Properties><Last-Modified>Thu, 14 Jan 2021 23:05:18 GMT</Last-Modified><Etag>\"0x8D8B8E0DC694EC9\"</Etag><LeaseStatus>unlocked</LeaseStatus><LeaseState>available</LeaseState><DefaultEncryptionScope>$account-encryption-key</DefaultEncryptionScope><DenyEncryptionScopeOverride>false</DenyEncryptionScopeOverride><HasImmutabilityPolicy>false</HasImmutabilityPolicy><HasLegalHold>false</HasLegalHold></Properties></Container></Containers><NextMarker /></EnumerationResults>",
"Date" : "Thu, 14 Jan 2021 23:05:17 GMT",
"x-ms-client-request-id" : "3291daa7-4a00-4174-9eb8-76902a002798",
"Content-Type" : "application/xml"
},
"Exception" : null
}, {
"Method" : "DELETE",
"Uri" : "https://REDACTED.blob.core.windows.net/jtcutilityconvertstreamtobufferavailable069264aaa25c09?restype=container",
"Headers" : {
"x-ms-version" : "2020-04-08",
"User-Agent" : "azsdk-java-azure-storage-blob/12.10.0-beta.2 (11.0.6; Windows 10; 10.0)",
"x-ms-client-request-id" : "dd95cd1b-a2df-4e06-91e3-c00a224c1108"
},
"Response" : {
"x-ms-version" : "2020-04-08",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"retry-after" : "0",
"Content-Length" : "0",
"StatusCode" : "202",
"x-ms-request-id" : "4f614636-701e-0041-13c9-eaaec5000000",
"Date" : "Thu, 14 Jan 2021 23:05:17 GMT",
"x-ms-client-request-id" : "dd95cd1b-a2df-4e06-91e3-c00a224c1108"
},
"Exception" : null
} ],
"variables" : [ "jtcutilityconvertstreamtobufferavailable069264aaa25c09", "bb652b56-674d-40a7-b572-fe934db49931" ]
}
1 change: 1 addition & 0 deletions sdk/storage/azure-storage-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 12.11.0-beta.1 (Unreleased)
- Removed a deep copy in PayloadSizeGate
- Fixed a bug that would throw if uploading using a stream that returned a number > 0 from available() after the stream had ended

## 12.10.0 (2021-01-14)
- GA release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public static Flux<ByteBuffer> convertStreamToByteBuffer(InputStream data, long
}))
.doOnComplete(() -> {
try {
if (data.available() > 0) {
if (data.read() != -1) {
long totalLength = currentTotalLength[0] + data.available();
throw LOGGER.logExceptionAsError(new UnexpectedLengthException(
String.format("Request body emitted %d bytes, more than the expected %d bytes.",
Expand Down