diff --git a/sdk/storage/Azure.Storage.Blobs/tests/BlobsClientTestFixtureAttribute.cs b/sdk/storage/Azure.Storage.Blobs/tests/BlobsClientTestFixtureAttribute.cs index bf42ec9151e6e..cd9014765cf44 100644 --- a/sdk/storage/Azure.Storage.Blobs/tests/BlobsClientTestFixtureAttribute.cs +++ b/sdk/storage/Azure.Storage.Blobs/tests/BlobsClientTestFixtureAttribute.cs @@ -7,23 +7,28 @@ namespace Azure.Storage.Blobs.Tests { public class BlobsClientTestFixtureAttribute : ClientTestFixtureAttribute { - public BlobsClientTestFixtureAttribute() + public BlobsClientTestFixtureAttribute(params object[] additionalParameters) : base( - BlobClientOptions.ServiceVersion.V2019_02_02, - BlobClientOptions.ServiceVersion.V2019_07_07, - BlobClientOptions.ServiceVersion.V2019_12_12, - BlobClientOptions.ServiceVersion.V2020_02_10, - BlobClientOptions.ServiceVersion.V2020_04_08, - BlobClientOptions.ServiceVersion.V2020_06_12, - BlobClientOptions.ServiceVersion.V2020_08_04, - BlobClientOptions.ServiceVersion.V2020_10_02, - BlobClientOptions.ServiceVersion.V2020_12_06, - BlobClientOptions.ServiceVersion.V2021_02_12, - BlobClientOptions.ServiceVersion.V2021_04_10, - BlobClientOptions.ServiceVersion.V2021_06_08, - BlobClientOptions.ServiceVersion.V2021_08_06, - StorageVersionExtensions.LatestVersion, - StorageVersionExtensions.MaxVersion) + serviceVersions: new object[] + { + BlobClientOptions.ServiceVersion.V2019_02_02, + BlobClientOptions.ServiceVersion.V2019_07_07, + BlobClientOptions.ServiceVersion.V2019_12_12, + BlobClientOptions.ServiceVersion.V2020_02_10, + BlobClientOptions.ServiceVersion.V2020_04_08, + BlobClientOptions.ServiceVersion.V2020_06_12, + BlobClientOptions.ServiceVersion.V2020_08_04, + BlobClientOptions.ServiceVersion.V2020_10_02, + BlobClientOptions.ServiceVersion.V2020_12_06, + BlobClientOptions.ServiceVersion.V2021_02_12, + BlobClientOptions.ServiceVersion.V2021_04_10, + BlobClientOptions.ServiceVersion.V2021_06_08, + BlobClientOptions.ServiceVersion.V2021_08_06, + StorageVersionExtensions.LatestVersion, + StorageVersionExtensions.MaxVersion + }, + additionalParameters: additionalParameters + ) { RecordingServiceVersion = StorageVersionExtensions.MaxVersion; LiveServiceVersions = new object[] { StorageVersionExtensions.LatestVersion }; diff --git a/sdk/storage/Azure.Storage.Blobs/tests/ClientSideEncryptedBlobClientOpenWriteTests.cs b/sdk/storage/Azure.Storage.Blobs/tests/ClientSideEncryptedBlobClientOpenWriteTests.cs index b2693e7886657..ca53b733cc9e2 100644 --- a/sdk/storage/Azure.Storage.Blobs/tests/ClientSideEncryptedBlobClientOpenWriteTests.cs +++ b/sdk/storage/Azure.Storage.Blobs/tests/ClientSideEncryptedBlobClientOpenWriteTests.cs @@ -5,6 +5,7 @@ using System.IO; using System.Threading.Tasks; using Azure.Core.TestFramework; +using Azure.Storage.Cryptography; using Azure.Storage.Test; using Azure.Storage.Test.Shared; using NUnit.Framework; @@ -19,17 +20,16 @@ namespace Azure.Storage.Blobs.Tests /// [LiveOnly] #pragma warning disable CS0618 // obsolete - [TestFixture(ClientSideEncryptionVersion.V1_0)] - [TestFixture(ClientSideEncryptionVersion.V2_0)] + [BlobsClientTestFixture(ClientSideEncryptionVersion.V1_0, ClientSideEncryptionVersion.V2_0)] #pragma warning restore CS0618 // obsolete public class ClientSideEncryptedBlobClientOpenWriteTests : BlobClientOpenWriteTests { private readonly ClientSideEncryptionVersion _version; public ClientSideEncryptedBlobClientOpenWriteTests( - ClientSideEncryptionVersion version, bool async, - BlobClientOptions.ServiceVersion serviceVersion) + BlobClientOptions.ServiceVersion serviceVersion, + ClientSideEncryptionVersion version) : base(async, serviceVersion, null /* RecordedTestMode.Record /* to re-record */) { _version = version; @@ -53,6 +53,15 @@ protected override BlobClient GetResourceClient(BlobContainerClient container, s return base.GetResourceClient(container, resourceName, options); } + protected override long GetExpectedDataLength(long dataLength) => _version switch + { +#pragma warning disable CS0618 // obsolete + ClientSideEncryptionVersion.V1_0 => ClientSideEncryptorV1_0.CalculateExpectedOutputContentLength(dataLength), +#pragma warning restore CS0618 // obsolete + ClientSideEncryptionVersion.V2_0 => ClientSideEncryptorV2_0.CalculateExpectedOutputContentLength(dataLength), + _ => dataLength + }; + #region Test Overrides /// /// Need to change assertions for a metadata test. @@ -122,43 +131,6 @@ public override async Task OpenWriteAsync_NewBlob_WithMetadata() await (AdditionalAssertions?.Invoke(client) ?? Task.CompletedTask); } - - /// - /// Need to change assertions for a progress reporting test. - /// Client-side encryption alters data length. - /// - [Test] - public override async Task OpenWriteAsync_ProgressReporting() - { - const int bufferSize = 256; - - // Arrange - await using IDisposingContainer disposingContainer = await GetDisposingContainerAsync(); - BlobClient client = GetResourceClient(disposingContainer.Container); - - byte[] data = GetRandomBuffer(Constants.KB); - using Stream stream = new MemoryStream(data); - - TestProgress progress = new TestProgress(); - - // Act - using (Stream openWriteStream = await OpenWriteAsync( - client, - overwrite: true, - maxDataSize: Constants.KB, - bufferSize: bufferSize, - progressHandler: progress)) - { - await stream.CopyToAsync(openWriteStream); - await openWriteStream.FlushAsync(); - } - - // Assert - Assert.IsTrue(progress.List.Count > 0); - Assert.AreEqual(data.Length - (data.Length % 16) + 16, progress.List[progress.List.Count - 1]); - - await (AdditionalAssertions?.Invoke(client) ?? Task.CompletedTask); - } #endregion } } diff --git a/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV1_0.cs b/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV1_0.cs index 4e4c9e87d0858..d164a6d766fb4 100644 --- a/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV1_0.cs +++ b/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV1_0.cs @@ -39,7 +39,9 @@ private void ValidateMembers() } } - public long ExpectedOutputContentLength(long plaintextLength) + public long ExpectedOutputContentLength(long plaintextLength) => CalculateExpectedOutputContentLength(plaintextLength); + + public static long CalculateExpectedOutputContentLength(long plaintextLength) { const int aesBlockSizeBytes = 16; diff --git a/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV2_0.cs b/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV2_0.cs index 86dbb948eb01b..06397104776e3 100644 --- a/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV2_0.cs +++ b/sdk/storage/Azure.Storage.Common/src/Shared/ClientsideEncryption/ClientSideEncryptorV2_0.cs @@ -33,7 +33,9 @@ public ClientSideEncryptorV2_0(ClientSideEncryptionOptions options) _keyWrapAlgorithm = options.KeyWrapAlgorithm; } - public long ExpectedOutputContentLength(long plaintextLength) + public long ExpectedOutputContentLength(long plaintextLength) => CalculateExpectedOutputContentLength(plaintextLength); + + public static long CalculateExpectedOutputContentLength(long plaintextLength) { long numBlocks = plaintextLength / EncryptionRegionDataSize; // partial block check diff --git a/sdk/storage/Azure.Storage.Common/tests/Shared/OpenWriteTestBase.cs b/sdk/storage/Azure.Storage.Common/tests/Shared/OpenWriteTestBase.cs index d05ba2b4fae7c..54038dc1035cc 100644 --- a/sdk/storage/Azure.Storage.Common/tests/Shared/OpenWriteTestBase.cs +++ b/sdk/storage/Azure.Storage.Common/tests/Shared/OpenWriteTestBase.cs @@ -149,6 +149,9 @@ protected string GetNewResourceName() private string GetGarbageLeaseId() => ClientBuilder.Recording.Random.NewGuid().ToString(); + // hook for clientside encryption to adjust some test assertions + protected virtual long GetExpectedDataLength(long dataLength) => dataLength; + #region Tests [RecordedTest] public async Task OpenWriteAsync_NewBlob() @@ -527,7 +530,7 @@ public virtual async Task OpenWriteAsync_ProgressReporting() // Assert Assert.IsTrue(progress.List.Count > 0); - Assert.AreEqual(dataSize, progress.List[progress.List.Count - 1]); + Assert.AreEqual(GetExpectedDataLength(dataSize), progress.List[progress.List.Count - 1]); await (AdditionalAssertions?.Invoke(client) ?? Task.CompletedTask); }