|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.storage.blob.specialized.cryptography; |
| 5 | + |
| 6 | +import com.azure.storage.common.implementation.Constants; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.GCM_ENCRYPTION_REGION_LENGTH; |
| 15 | +import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.NONCE_LENGTH; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | + |
| 19 | +public class EncryptedBlobLengthTests extends BlobCryptographyTestBase { |
| 20 | + |
| 21 | + //to prevent having to cast int -> long everywhere |
| 22 | + private static final long FOUR_MB = 4 * Constants.MB; |
| 23 | + private static final long SIXTEEN_MB = 16 * Constants.MB; |
| 24 | + private static final long ONE_MB = Constants.MB; |
| 25 | + private static final long ONE_KB = Constants.KB; |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @MethodSource("correctAdjustedLengthV1Supplier") |
| 29 | + public void correctAdjustedLengthV1(Long encryptedLength) { |
| 30 | + EncryptionData encryptionData = new EncryptionData(); |
| 31 | + encryptionData.setEncryptionAgent(new EncryptionAgent("1.0", null)); |
| 32 | + |
| 33 | + Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); |
| 34 | + assertEquals(encryptedLength, newLength); |
| 35 | + } |
| 36 | + |
| 37 | + private static Stream<Arguments> correctAdjustedLengthV1Supplier() { |
| 38 | + return Stream.of( |
| 39 | + Arguments.of(FOUR_MB), |
| 40 | + Arguments.of(SIXTEEN_MB) |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @MethodSource("correctAdjustedLengthV2Supplier") |
| 46 | + public void correctAdjustedLengthV2(Long encryptedLength, Long expectedLength) { |
| 47 | + EncryptionData encryptionData = new EncryptionData(); |
| 48 | + encryptionData.setEncryptionAgent(new EncryptionAgent("2.0", null)); |
| 49 | + encryptionData.setEncryptedRegionInfo(new EncryptedRegionInfo(GCM_ENCRYPTION_REGION_LENGTH, NONCE_LENGTH)); |
| 50 | + |
| 51 | + Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); |
| 52 | + assertEquals(expectedLength, newLength); |
| 53 | + } |
| 54 | + |
| 55 | + private static Stream<Arguments> correctAdjustedLengthV2Supplier() { |
| 56 | + return Stream.of( |
| 57 | + Arguments.of(FOUR_MB + 28, FOUR_MB), |
| 58 | + Arguments.of(FOUR_MB + 57, FOUR_MB + 1), |
| 59 | + Arguments.of(SIXTEEN_MB + 112, SIXTEEN_MB), |
| 60 | + Arguments.of(28L, 0L), |
| 61 | + Arguments.of(0L, 0L), |
| 62 | + Arguments.of(ONE_MB + 28, ONE_MB) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @ParameterizedTest |
| 67 | + @MethodSource("correctAdjustedLengthVariableRegionSupplier") |
| 68 | + public void correctAdjustedLengthVariableRegion(Long encryptedLength, Long expectedLength, Long regionLength) { |
| 69 | + EncryptionData encryptionData = new EncryptionData(); |
| 70 | + encryptionData.setEncryptionAgent(new EncryptionAgent("2.1", null)); |
| 71 | + encryptionData.setEncryptedRegionInfo(new EncryptedRegionInfo(regionLength, NONCE_LENGTH)); |
| 72 | + |
| 73 | + Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); |
| 74 | + assertEquals(expectedLength, newLength); |
| 75 | + } |
| 76 | + |
| 77 | + private static Stream<Arguments> correctAdjustedLengthVariableRegionSupplier() { |
| 78 | + return Stream.of( |
| 79 | + Arguments.of(FOUR_MB + 112, FOUR_MB, ONE_MB), |
| 80 | + Arguments.of(SIXTEEN_MB + 448, SIXTEEN_MB, ONE_MB), |
| 81 | + Arguments.of(FOUR_MB + 114688, FOUR_MB, ONE_KB), |
| 82 | + Arguments.of(SIXTEEN_MB + 458752, SIXTEEN_MB, ONE_KB), |
| 83 | + Arguments.of(FOUR_MB + 448, FOUR_MB, 256 * ONE_KB), |
| 84 | + Arguments.of(SIXTEEN_MB + 1792, SIXTEEN_MB, 256 * ONE_KB), |
| 85 | + Arguments.of(ONE_MB + 28672, ONE_MB, ONE_KB), |
| 86 | + Arguments.of(ONE_MB + 28, ONE_MB, ONE_MB) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void badProtocol() { |
| 92 | + EncryptionData encryptionData = new EncryptionData(); |
| 93 | + encryptionData.setEncryptionAgent(new EncryptionAgent("garbage", null)); |
| 94 | + |
| 95 | + assertThrows(IllegalArgumentException.class, () -> EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, |
| 96 | + null)); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments