-
Notifications
You must be signed in to change notification settings - Fork 311
Tests | Move fixtures to common project #3402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cheenamalhotra
merged 8 commits into
dotnet:main
from
edwardneal:tests/common-project-fixtures
Jun 24, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3503f00
Add Common project to solution
edwardneal 81be890
Enable default testing of net8.0
edwardneal 3aa4e18
Move fixtures to the Common tests project, update references
edwardneal d8722b6
Add high-level summary to cryptographic fixtures
edwardneal 87a461c
Use interpolated string in CspCertificateFixture
edwardneal b7fa2da
Merge branch 'main' into tests/common-project-fixtures
paulmedynski 86d22a7
Merge branch 'main' into tests/common-project-fixtures
edwardneal 15c5860
Correct merge of Common.csproj
edwardneal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Microsoft.Data.SqlClient/tests/Common/Fixtures/AzureKeyVaultKeyFixtureBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information.using System; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Azure.Core; | ||
using Azure.Security.KeyVault.Keys; | ||
|
||
namespace Microsoft.Data.SqlClient.Tests.Common.Fixtures; | ||
|
||
/// <summary> | ||
/// Provides a base class for managing Azure Key Vault keys in test fixtures. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class simplifies the creation and cleanup of RSA keys in an Azure Key Vault during testing | ||
/// scenarios. It ensures that any keys created during the fixture's lifetime are properly deleted when the fixture is | ||
/// disposed. | ||
/// </remarks> | ||
public abstract class AzureKeyVaultKeyFixtureBase : IDisposable | ||
{ | ||
private readonly KeyClient _keyClient; | ||
private readonly Random _randomGenerator; | ||
|
||
private readonly List<KeyVaultKey> _createdKeys = new List<KeyVaultKey>(); | ||
|
||
protected AzureKeyVaultKeyFixtureBase(Uri keyVaultUri, TokenCredential keyVaultToken) | ||
{ | ||
_keyClient = new KeyClient(keyVaultUri, keyVaultToken); | ||
_randomGenerator = new Random(); | ||
} | ||
|
||
protected Uri CreateKey(string name, int keySize) | ||
{ | ||
CreateRsaKeyOptions createOptions = new CreateRsaKeyOptions(GenerateUniqueName(name)) { KeySize = keySize }; | ||
KeyVaultKey created = _keyClient.CreateRsaKey(createOptions); | ||
|
||
_createdKeys.Add(created); | ||
return created.Id; | ||
} | ||
|
||
private string GenerateUniqueName(string name) | ||
{ | ||
byte[] rndBytes = new byte[16]; | ||
|
||
_randomGenerator.NextBytes(rndBytes); | ||
return name + "-" + BitConverter.ToString(rndBytes); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
foreach (KeyVaultKey key in _createdKeys) | ||
{ | ||
try | ||
{ | ||
_keyClient.StartDeleteKey(key.Name).WaitForCompletion(); | ||
} | ||
catch (Exception) | ||
{ | ||
continue; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.