Skip to content

Commit

Permalink
Features/storage/blob lease client request conditions validation (#22111
Browse files Browse the repository at this point in the history
)
  • Loading branch information
seanmcc-msft authored Jun 23, 2021
1 parent 302faea commit 246e9ef
Show file tree
Hide file tree
Showing 22 changed files with 323 additions and 30 deletions.
60 changes: 30 additions & 30 deletions sdk/storage/Azure.Storage.Blobs/src/BlobLeaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ private async Task<Response<BlobLease>> AcquireInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Acquire),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerAcquireLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -561,12 +561,12 @@ private async Task<Response<BlobLease>> RenewInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Release),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerRenewLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -784,12 +784,12 @@ public virtual async Task<Response<ReleasedObjectInfo>> ReleaseInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(RequestConditions.IfMatch),
nameof(RequestConditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Release),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerReleaseLeaseHeaders> response;

Expand Down Expand Up @@ -1009,12 +1009,12 @@ private async Task<Response<BlobLease>> ChangeInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Change),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerChangeLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -1293,12 +1293,12 @@ private async Task<Response<BlobLease>> BreakInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Break),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerBreakLeaseHeaders> response;

Expand Down
173 changes: 173 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,42 @@ public async Task AcquireLeaseAsync()
await container.DeleteIfExistsAsync(conditions: new BlobRequestConditions { LeaseId = response.Value.LeaseId });
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task AcquireLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
TimeSpan duration = TimeSpan.FromSeconds(15);
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.AcquireAsync(
duration,
conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Acquire does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task AcquireLeaseAsync_ErrorDurationTooLarge()
{
Expand Down Expand Up @@ -1577,6 +1613,40 @@ public async Task RenewLeaseAsync()
await container.DeleteIfExistsAsync(conditions: new BlobRequestConditions { LeaseId = renewResponse.Value.LeaseId });
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task RenewLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.RenewAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Release does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task RenewLeaseAsync_Error()
{
Expand Down Expand Up @@ -1671,6 +1741,40 @@ public async Task ReleaseLeaseAsync()
Assert.AreEqual(LeaseState.Available, response.Value.LeaseState);
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task ReleaseLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.ReleaseAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Release does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task ReleaseLeaseAsync_Error()
{
Expand Down Expand Up @@ -1762,6 +1866,40 @@ public async Task BreakLeaseAsync()
Assert.AreEqual(LeaseState.Broken, response.Value.LeaseState);
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task BreakLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.BreakAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Break does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task BreakLeaseAsync_Error()
{
Expand Down Expand Up @@ -1861,6 +1999,41 @@ public async Task ChangeLeaseAsync()
await InstrumentClient(test.Container.GetBlobLeaseClient(changeResponse.Value.LeaseId)).ReleaseAsync();
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task ChangeLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.ChangeAsync(
id,
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Change does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task ChangeLeaseAsync_Error()
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 246e9ef

Please sign in to comment.