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