Skip to content

Commit

Permalink
fix test construction (#29700)
Browse files Browse the repository at this point in the history
* fix test construction

augmented blob test fixture to accept additional parameters
reorder test constructor args

* fix test
  • Loading branch information
jaschrep-msft authored Jul 7, 2022
1 parent 4586797 commit ad4d05e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,17 +20,16 @@ namespace Azure.Storage.Blobs.Tests
/// </summary>
[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;
Expand All @@ -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
/// <summary>
/// Need to change assertions for a metadata test.
Expand Down Expand Up @@ -122,43 +131,6 @@ public override async Task OpenWriteAsync_NewBlob_WithMetadata()

await (AdditionalAssertions?.Invoke(client) ?? Task.CompletedTask);
}

/// <summary>
/// Need to change assertions for a progress reporting test.
/// Client-side encryption alters data length.
/// </summary>
[Test]
public override async Task OpenWriteAsync_ProgressReporting()
{
const int bufferSize = 256;

// Arrange
await using IDisposingContainer<BlobContainerClient> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ad4d05e

Please sign in to comment.