Skip to content

Commit f47ad32

Browse files
trwalketrwalke
andauthored
Fix for test issue for EnsureDefaultCacheKeyBehaviorWhenCertSerialNumberIsNotUsedTestAsync (#5158)
Fix for test issue Co-authored-by: trwalke <[email protected]>
1 parent 68dc10f commit f47ad32

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

tests/Microsoft.Identity.Test.Unit/PublicApiTests/ClientCredentialWithCertTest.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ namespace Microsoft.Identity.Test.Unit
3030
[DeploymentItem(@"Resources\RSATestCertDotNet.pfx")]
3131
public class ConfidentialClientWithCertTests : TestBase
3232
{
33+
private byte[] _serializedCache;
34+
3335
[TestInitialize]
3436
public override void TestInitialize()
3537
{
3638
base.TestInitialize();
39+
_serializedCache = null;
3740
}
3841

3942
private static MockHttpMessageHandler CreateTokenResponseHttpHandler(bool clientCredentialFlow)
@@ -717,11 +720,11 @@ public async Task EnsureCertificateSerialNumberIsAddedToCacheKeyTestAsync()
717720
.WithCertificate(certificate, true, true)
718721
.WithHttpManager(httpManager)
719722
.WithExperimentalFeatures()
720-
.WithCacheOptions(options: new CacheOptions() { UseSharedCache = true })
721723
.BuildConcrete();
722724

723-
//Clear static caches
724-
app.AppTokenCacheInternal.Accessor.Clear();
725+
app.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
726+
app.AppTokenCache.SetAfterAccess(AfterCacheAccess);
727+
725728
var appCacheAccess = app.AppTokenCache.RecordAccess();
726729

727730
httpManager.AddInstanceDiscoveryMockHandler();
@@ -758,10 +761,12 @@ public async Task EnsureCertificateSerialNumberIsAddedToCacheKeyTestAsync()
758761
.WithRedirectUri(TestConstants.RedirectUri)
759762
.WithCertificate(certificate, true, true)
760763
.WithHttpManager(httpManager)
761-
.WithCacheOptions(options: new CacheOptions() { UseSharedCache = true })
762764
.WithExperimentalFeatures()
763765
.BuildConcrete();
764766

767+
app2.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
768+
app2.AppTokenCache.SetAfterAccess(AfterCacheAccess);
769+
765770
appCacheAccess = app2.AppTokenCache.RecordAccess();
766771

767772
//Ensure serial number matches
@@ -785,10 +790,12 @@ public async Task EnsureCertificateSerialNumberIsAddedToCacheKeyTestAsync()
785790
.WithRedirectUri(TestConstants.RedirectUri)
786791
.WithCertificate(certificate2, true, true)
787792
.WithHttpManager(httpManager)
788-
.WithCacheOptions(options: new CacheOptions() { UseSharedCache = true })
789793
.WithExperimentalFeatures()
790794
.BuildConcrete();
791795

796+
app3.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
797+
app3.AppTokenCache.SetAfterAccess(AfterCacheAccess);
798+
792799
//Ensure serial number does not match
793800
result = await app3.AcquireTokenForClient(TestConstants.s_scope.ToArray())
794801
.ExecuteAsync(CancellationToken.None)
@@ -814,11 +821,10 @@ public async Task EnsureDefaultCacheKeyBehaviorWhenCertSerialNumberIsNotUsedTest
814821
.WithCertificate(certificate, true, false)
815822
.WithHttpManager(httpManager)
816823
.WithExperimentalFeatures()
817-
.WithCacheOptions(options: new CacheOptions() { UseSharedCache = true })
818824
.BuildConcrete();
819825

820-
//Clear static caches
821-
app.AppTokenCacheInternal.Accessor.Clear();
826+
app.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
827+
app.AppTokenCache.SetAfterAccess(AfterCacheAccess);
822828

823829
var appCacheAccess = app.AppTokenCache.RecordAccess();
824830

@@ -850,10 +856,12 @@ public async Task EnsureDefaultCacheKeyBehaviorWhenCertSerialNumberIsNotUsedTest
850856
.WithRedirectUri(TestConstants.RedirectUri)
851857
.WithCertificate(certificate, true, true)
852858
.WithHttpManager(httpManager)
853-
.WithCacheOptions(options: new CacheOptions() { UseSharedCache = true })
854859
.WithExperimentalFeatures()
855860
.BuildConcrete();
856861

862+
app2.AppTokenCache.SetBeforeAccess(BeforeCacheAccess);
863+
app2.AppTokenCache.SetAfterAccess(AfterCacheAccess);
864+
857865
app2.AppTokenCache.RecordAccess();
858866

859867
result = await app2.AcquireTokenForClient(TestConstants.s_scope.ToArray())
@@ -864,8 +872,8 @@ public async Task EnsureDefaultCacheKeyBehaviorWhenCertSerialNumberIsNotUsedTest
864872
Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource);
865873
Assert.AreEqual("header.payload.signature", result.AccessToken);
866874

867-
var serialNumber = app.AppTokenCacheInternal.Accessor.GetAllAccessTokens().First().AdditionalCacheKeyComponents.FirstOrDefault().Value;
868-
Assert.AreEqual(certificate.SerialNumber, serialNumber);
875+
var token = app2.AppTokenCacheInternal.Accessor.GetAllAccessTokens().Where(x => x.AdditionalCacheKeyComponents.Any()).FirstOrDefault();
876+
Assert.AreEqual(certificate.SerialNumber, token.AdditionalCacheKeyComponents.FirstOrDefault().Value);
869877
}
870878
}
871879

@@ -891,6 +899,16 @@ public void EnsureNullCertDoesNotSetSerialNumberTestAsync()
891899
}
892900
}
893901

902+
private void BeforeCacheAccess(TokenCacheNotificationArgs args)
903+
{
904+
args.TokenCache.DeserializeMsalV3(_serializedCache);
905+
}
906+
907+
private void AfterCacheAccess(TokenCacheNotificationArgs args)
908+
{
909+
_serializedCache = args.TokenCache.SerializeMsalV3();
910+
}
911+
894912
private static string ComputeCertThumbprint(X509Certificate2 certificate, bool useSha2)
895913
{
896914
string thumbprint = null;

0 commit comments

Comments
 (0)