|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.storage.blob; |
| 5 | + |
| 6 | +import com.azure.core.test.utils.TestUtils; |
| 7 | +import com.azure.core.util.FluxUtil; |
| 8 | +import com.azure.storage.blob.models.BlobRange; |
| 9 | +import com.azure.storage.common.DownloadContentValidationOptions; |
| 10 | +import com.azure.storage.common.implementation.Constants; |
| 11 | +import com.azure.storage.common.implementation.structuredmessage.StructuredMessageEncoder; |
| 12 | +import com.azure.storage.common.implementation.structuredmessage.StructuredMessageFlags; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import reactor.core.publisher.Flux; |
| 16 | +import reactor.test.StepVerifier; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.ByteBuffer; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests for structured message decoding during blob downloads using StorageContentValidationDecoderPolicy. |
| 26 | + * These tests verify that the pipeline policy correctly decodes structured messages when content validation is enabled. |
| 27 | + */ |
| 28 | +public class BlobMessageDecoderDownloadTests extends BlobTestBase { |
| 29 | + |
| 30 | + private BlobAsyncClient bc; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void setup() { |
| 34 | + String blobName = generateBlobName(); |
| 35 | + bc = ccAsync.getBlobAsyncClient(blobName); |
| 36 | + bc.upload(Flux.just(ByteBuffer.wrap(new byte[0])), null).block(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void downloadStreamWithResponseContentValidation() throws IOException { |
| 41 | + byte[] randomData = getRandomByteArray(Constants.KB); |
| 42 | + StructuredMessageEncoder encoder |
| 43 | + = new StructuredMessageEncoder(randomData.length, 512, StructuredMessageFlags.STORAGE_CRC64); |
| 44 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 45 | + |
| 46 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 47 | + |
| 48 | + DownloadContentValidationOptions validationOptions |
| 49 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 50 | + |
| 51 | + StepVerifier |
| 52 | + .create(bc.upload(input, null, true) |
| 53 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 54 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))) |
| 55 | + .assertNext(r -> TestUtils.assertArraysEqual(r, randomData)) |
| 56 | + .verifyComplete(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void downloadStreamWithResponseContentValidationRange() throws IOException { |
| 61 | + byte[] randomData = getRandomByteArray(Constants.KB); |
| 62 | + StructuredMessageEncoder encoder |
| 63 | + = new StructuredMessageEncoder(randomData.length, 512, StructuredMessageFlags.STORAGE_CRC64); |
| 64 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 65 | + |
| 66 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 67 | + |
| 68 | + DownloadContentValidationOptions validationOptions |
| 69 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 70 | + |
| 71 | + BlobRange range = new BlobRange(0, 512L); |
| 72 | + |
| 73 | + StepVerifier.create(bc.upload(input, null, true) |
| 74 | + .then(bc.downloadStreamWithResponse(range, null, null, false, validationOptions)) |
| 75 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))).assertNext(r -> { |
| 76 | + assertNotNull(r); |
| 77 | + assertTrue(r.length > 0); |
| 78 | + }).verifyComplete(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void downloadStreamWithResponseContentValidationLargeBlob() throws IOException { |
| 83 | + // Test with larger data to verify chunking works correctly |
| 84 | + byte[] randomData = getRandomByteArray(5 * Constants.KB); |
| 85 | + StructuredMessageEncoder encoder |
| 86 | + = new StructuredMessageEncoder(randomData.length, 1024, StructuredMessageFlags.STORAGE_CRC64); |
| 87 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 88 | + |
| 89 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 90 | + |
| 91 | + DownloadContentValidationOptions validationOptions |
| 92 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 93 | + |
| 94 | + StepVerifier |
| 95 | + .create(bc.upload(input, null, true) |
| 96 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 97 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))) |
| 98 | + .assertNext(r -> TestUtils.assertArraysEqual(r, randomData)) |
| 99 | + .verifyComplete(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void downloadStreamWithResponseContentValidationMultipleSegments() throws IOException { |
| 104 | + // Test with multiple segments to ensure all segments are decoded correctly |
| 105 | + byte[] randomData = getRandomByteArray(2 * Constants.KB); |
| 106 | + StructuredMessageEncoder encoder |
| 107 | + = new StructuredMessageEncoder(randomData.length, 512, StructuredMessageFlags.STORAGE_CRC64); |
| 108 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 109 | + |
| 110 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 111 | + |
| 112 | + DownloadContentValidationOptions validationOptions |
| 113 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 114 | + |
| 115 | + StepVerifier |
| 116 | + .create(bc.upload(input, null, true) |
| 117 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 118 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))) |
| 119 | + .assertNext(r -> TestUtils.assertArraysEqual(r, randomData)) |
| 120 | + .verifyComplete(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void downloadStreamWithResponseNoValidation() throws IOException { |
| 125 | + // Test that download works normally when validation is not enabled |
| 126 | + byte[] randomData = getRandomByteArray(Constants.KB); |
| 127 | + StructuredMessageEncoder encoder |
| 128 | + = new StructuredMessageEncoder(randomData.length, 512, StructuredMessageFlags.STORAGE_CRC64); |
| 129 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 130 | + |
| 131 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 132 | + |
| 133 | + // No validation options - should download encoded data as-is |
| 134 | + StepVerifier.create(bc.upload(input, null, true) |
| 135 | + .then(bc.downloadStreamWithResponse(null, null, null, false)) |
| 136 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))).assertNext(r -> { |
| 137 | + assertNotNull(r); |
| 138 | + // Should get encoded data, not decoded |
| 139 | + assertTrue(r.length > randomData.length); // Encoded data is larger |
| 140 | + }).verifyComplete(); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void downloadStreamWithResponseValidationDisabled() throws IOException { |
| 145 | + // Test with validation options but validation disabled |
| 146 | + byte[] randomData = getRandomByteArray(Constants.KB); |
| 147 | + StructuredMessageEncoder encoder |
| 148 | + = new StructuredMessageEncoder(randomData.length, 512, StructuredMessageFlags.STORAGE_CRC64); |
| 149 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 150 | + |
| 151 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 152 | + |
| 153 | + DownloadContentValidationOptions validationOptions |
| 154 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(false); |
| 155 | + |
| 156 | + StepVerifier.create(bc.upload(input, null, true) |
| 157 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 158 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))).assertNext(r -> { |
| 159 | + assertNotNull(r); |
| 160 | + // Should get encoded data, not decoded |
| 161 | + assertTrue(r.length > randomData.length); // Encoded data is larger |
| 162 | + }).verifyComplete(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void downloadStreamWithResponseContentValidationSmallSegment() throws IOException { |
| 167 | + // Test with small segment size to ensure boundary conditions are handled |
| 168 | + byte[] randomData = getRandomByteArray(256); |
| 169 | + StructuredMessageEncoder encoder |
| 170 | + = new StructuredMessageEncoder(randomData.length, 128, StructuredMessageFlags.STORAGE_CRC64); |
| 171 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 172 | + |
| 173 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 174 | + |
| 175 | + DownloadContentValidationOptions validationOptions |
| 176 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 177 | + |
| 178 | + StepVerifier |
| 179 | + .create(bc.upload(input, null, true) |
| 180 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 181 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))) |
| 182 | + .assertNext(r -> TestUtils.assertArraysEqual(r, randomData)) |
| 183 | + .verifyComplete(); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void downloadStreamWithResponseContentValidationVeryLargeBlob() throws IOException { |
| 188 | + // Test with very large data to verify chunking and policy work correctly with large blobs |
| 189 | + byte[] randomData = getRandomByteArray(10 * Constants.KB); |
| 190 | + StructuredMessageEncoder encoder |
| 191 | + = new StructuredMessageEncoder(randomData.length, 2048, StructuredMessageFlags.STORAGE_CRC64); |
| 192 | + ByteBuffer encodedData = encoder.encode(ByteBuffer.wrap(randomData)); |
| 193 | + |
| 194 | + Flux<ByteBuffer> input = Flux.just(encodedData); |
| 195 | + |
| 196 | + DownloadContentValidationOptions validationOptions |
| 197 | + = new DownloadContentValidationOptions().setStructuredMessageValidationEnabled(true); |
| 198 | + |
| 199 | + StepVerifier |
| 200 | + .create(bc.upload(input, null, true) |
| 201 | + .then(bc.downloadStreamWithResponse(null, null, null, false, validationOptions)) |
| 202 | + .flatMap(r -> FluxUtil.collectBytesInByteBufferStream(r.getValue()))) |
| 203 | + .assertNext(r -> TestUtils.assertArraysEqual(r, randomData)) |
| 204 | + .verifyComplete(); |
| 205 | + } |
| 206 | +} |
0 commit comments