Skip to content

Commit

Permalink
trying fix for encryption test (#3066)
Browse files Browse the repository at this point in the history
* trying fix

* Fix test issues with null refs

* Update Microsoft.Extensions.Caching.Memory versions

* Fix warnings that would become TSA issues

---------

Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
  • Loading branch information
JoshLozensky and jmprieur authored Oct 8, 2024
1 parent dfb9cc3 commit cc33716
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<MicrosoftAspNetCoreAuthenticationJwtBearerVersion>8.0.0</MicrosoftAspNetCoreAuthenticationJwtBearerVersion>
<MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>8.0.0</MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>
<MicrosoftExtensionsCachingMemoryVersion>8.0.0</MicrosoftExtensionsCachingMemoryVersion>
<MicrosoftExtensionsCachingMemoryVersion>8.0.1</MicrosoftExtensionsCachingMemoryVersion>
<MicrosoftExtensionsHostingVersion>8.0.0</MicrosoftExtensionsHostingVersion>
<MicrosoftAspNetCoreDataProtectionVersion>8.0.1</MicrosoftAspNetCoreDataProtectionVersion>
<SystemSecurityCryptographyPkcsVersion>8.0.0</SystemSecurityCryptographyPkcsVersion>
Expand Down Expand Up @@ -144,7 +144,7 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<MicrosoftAspNetCoreAuthenticationJwtBearerVersion>6.0.12</MicrosoftAspNetCoreAuthenticationJwtBearerVersion>
<MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>6.0.12</MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>
<MicrosoftExtensionsCachingMemoryVersion>6.0.0</MicrosoftExtensionsCachingMemoryVersion>
<MicrosoftExtensionsCachingMemoryVersion>6.0.2</MicrosoftExtensionsCachingMemoryVersion>
<MicrosoftExtensionsHostingVersion>6.0.0</MicrosoftExtensionsHostingVersion>
<MicrosoftAspNetCoreDataProtectionVersion>6.0.0</MicrosoftAspNetCoreDataProtectionVersion>
<SystemSecurityCryptographyXmlVersion>6.0.1</SystemSecurityCryptographyXmlVersion>
Expand Down
27 changes: 19 additions & 8 deletions tests/Microsoft.Identity.Web.Test/CacheEncryptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ namespace Microsoft.Identity.Web.Test
{
public class CacheEncryptionTests
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
// These fields won't be null in tests, as tests call BuildTheRequiredServices()
private TestMsalDistributedTokenCacheAdapter _testCacheAdapter;
private IServiceProvider _provider;

#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private TestMsalDistributedTokenCacheAdapter? _testCacheAdapter;
private IServiceProvider? _provider;

[Theory]
[InlineData(true)]
Expand All @@ -35,7 +32,7 @@ public async Task EncryptionTestAsync(bool isEncrypted)
// Arrange
byte[] cache = new byte[] { 1, 2, 3, 4 };
BuildTheRequiredServices(isEncrypted);
_testCacheAdapter = (_provider.GetRequiredService<IMsalTokenCacheProvider>() as TestMsalDistributedTokenCacheAdapter)!;
_testCacheAdapter = (_provider!.GetRequiredService<IMsalTokenCacheProvider>() as TestMsalDistributedTokenCacheAdapter)!;
TestTokenCache tokenCache = new TestTokenCache();
TokenCacheNotificationArgs args = InstantiateTokenCacheNotificationArgs(tokenCache);
_testCacheAdapter.Initialize(tokenCache);
Expand All @@ -54,7 +51,12 @@ public async Task EncryptionTestAsync(bool isEncrypted)
private byte[] GetFirstCacheValue(MemoryCache memoryCache)
{
IDictionary memoryCacheContent;
#if NET7_0_OR_GREATER
# if NET6_0
memoryCacheContent = (memoryCache
.GetType()
.GetProperty("StringKeyEntriesCollection", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
.GetValue(_testCacheAdapter!._memoryCache) as IDictionary)!;
#elif NET7_0
dynamic content1 = memoryCache
.GetType()
.GetField("_coherentState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
Expand All @@ -63,11 +65,20 @@ private byte[] GetFirstCacheValue(MemoryCache memoryCache)
.GetType()
.GetField("_entries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.GetValue(content1) as IDictionary)!;
#elif NET8_0_OR_GREATER
dynamic content1 = memoryCache
.GetType()
.GetField("_coherentState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
.GetValue(memoryCache)!;
memoryCacheContent = (content1?
.GetType()
.GetField("_stringEntries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.GetValue(content1) as IDictionary)!;
#else
memoryCacheContent = (memoryCache
.GetType()
.GetField("_entries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
.GetValue(_testCacheAdapter._memoryCache) as IDictionary)!;
.GetValue(_testCacheAdapter!._memoryCache) as IDictionary)!;
#endif
var firstEntry = memoryCacheContent.Values.OfType<object>().First();
var firstEntryValue = firstEntry.GetType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public async Task SerializeInput_WithSerializer_ReturnsSerializedContent_WhenJso

// Assert
Assert.NotNull(result);
Assert.Equal("serialized", await (result?.ReadAsStringAsync()));
Assert.Equal("serialized", await (result?.ReadAsStringAsync()!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void AddTokenAcquisition_AbleToOverrideICredentialsLoader()

ServiceDescriptor[] orderedServices = services.OrderBy(s => s.ServiceType.FullName).ToArray();

Assert.Single(orderedServices.Where(s => s.ServiceType == typeof(ICredentialsLoader)));
Assert.Single(orderedServices, s => s.ServiceType == typeof(ICredentialsLoader));
}

[Fact]
Expand Down

0 comments on commit cc33716

Please sign in to comment.