Skip to content

Commit cc33716

Browse files
trying fix for encryption test (#3066)
* 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 <[email protected]>
1 parent dfb9cc3 commit cc33716

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
117117
<MicrosoftAspNetCoreAuthenticationJwtBearerVersion>8.0.0</MicrosoftAspNetCoreAuthenticationJwtBearerVersion>
118118
<MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>8.0.0</MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>
119-
<MicrosoftExtensionsCachingMemoryVersion>8.0.0</MicrosoftExtensionsCachingMemoryVersion>
119+
<MicrosoftExtensionsCachingMemoryVersion>8.0.1</MicrosoftExtensionsCachingMemoryVersion>
120120
<MicrosoftExtensionsHostingVersion>8.0.0</MicrosoftExtensionsHostingVersion>
121121
<MicrosoftAspNetCoreDataProtectionVersion>8.0.1</MicrosoftAspNetCoreDataProtectionVersion>
122122
<SystemSecurityCryptographyPkcsVersion>8.0.0</SystemSecurityCryptographyPkcsVersion>
@@ -144,7 +144,7 @@
144144
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
145145
<MicrosoftAspNetCoreAuthenticationJwtBearerVersion>6.0.12</MicrosoftAspNetCoreAuthenticationJwtBearerVersion>
146146
<MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>6.0.12</MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>
147-
<MicrosoftExtensionsCachingMemoryVersion>6.0.0</MicrosoftExtensionsCachingMemoryVersion>
147+
<MicrosoftExtensionsCachingMemoryVersion>6.0.2</MicrosoftExtensionsCachingMemoryVersion>
148148
<MicrosoftExtensionsHostingVersion>6.0.0</MicrosoftExtensionsHostingVersion>
149149
<MicrosoftAspNetCoreDataProtectionVersion>6.0.0</MicrosoftAspNetCoreDataProtectionVersion>
150150
<SystemSecurityCryptographyXmlVersion>6.0.1</SystemSecurityCryptographyXmlVersion>

tests/Microsoft.Identity.Web.Test/CacheEncryptionTests.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ namespace Microsoft.Identity.Web.Test
2020
{
2121
public class CacheEncryptionTests
2222
{
23-
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
2423
// These fields won't be null in tests, as tests call BuildTheRequiredServices()
25-
private TestMsalDistributedTokenCacheAdapter _testCacheAdapter;
26-
private IServiceProvider _provider;
27-
28-
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
24+
private TestMsalDistributedTokenCacheAdapter? _testCacheAdapter;
25+
private IServiceProvider? _provider;
2926

3027
[Theory]
3128
[InlineData(true)]
@@ -35,7 +32,7 @@ public async Task EncryptionTestAsync(bool isEncrypted)
3532
// Arrange
3633
byte[] cache = new byte[] { 1, 2, 3, 4 };
3734
BuildTheRequiredServices(isEncrypted);
38-
_testCacheAdapter = (_provider.GetRequiredService<IMsalTokenCacheProvider>() as TestMsalDistributedTokenCacheAdapter)!;
35+
_testCacheAdapter = (_provider!.GetRequiredService<IMsalTokenCacheProvider>() as TestMsalDistributedTokenCacheAdapter)!;
3936
TestTokenCache tokenCache = new TestTokenCache();
4037
TokenCacheNotificationArgs args = InstantiateTokenCacheNotificationArgs(tokenCache);
4138
_testCacheAdapter.Initialize(tokenCache);
@@ -54,7 +51,12 @@ public async Task EncryptionTestAsync(bool isEncrypted)
5451
private byte[] GetFirstCacheValue(MemoryCache memoryCache)
5552
{
5653
IDictionary memoryCacheContent;
57-
#if NET7_0_OR_GREATER
54+
# if NET6_0
55+
memoryCacheContent = (memoryCache
56+
.GetType()
57+
.GetProperty("StringKeyEntriesCollection", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
58+
.GetValue(_testCacheAdapter!._memoryCache) as IDictionary)!;
59+
#elif NET7_0
5860
dynamic content1 = memoryCache
5961
.GetType()
6062
.GetField("_coherentState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
@@ -63,11 +65,20 @@ private byte[] GetFirstCacheValue(MemoryCache memoryCache)
6365
.GetType()
6466
.GetField("_entries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
6567
.GetValue(content1) as IDictionary)!;
68+
#elif NET8_0_OR_GREATER
69+
dynamic content1 = memoryCache
70+
.GetType()
71+
.GetField("_coherentState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
72+
.GetValue(memoryCache)!;
73+
memoryCacheContent = (content1?
74+
.GetType()
75+
.GetField("_stringEntries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
76+
.GetValue(content1) as IDictionary)!;
6677
#else
6778
memoryCacheContent = (memoryCache
6879
.GetType()
6980
.GetField("_entries", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
70-
.GetValue(_testCacheAdapter._memoryCache) as IDictionary)!;
81+
.GetValue(_testCacheAdapter!._memoryCache) as IDictionary)!;
7182
#endif
7283
var firstEntry = memoryCacheContent.Values.OfType<object>().First();
7384
var firstEntryValue = firstEntry.GetType()

tests/Microsoft.Identity.Web.Test/DownstreamWebApiSupport/DownstreamApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public async Task SerializeInput_WithSerializer_ReturnsSerializedContent_WhenJso
291291

292292
// Assert
293293
Assert.NotNull(result);
294-
Assert.Equal("serialized", await (result?.ReadAsStringAsync()));
294+
Assert.Equal("serialized", await (result?.ReadAsStringAsync()!));
295295
}
296296

297297
[Fact]

tests/Microsoft.Identity.Web.Test/ServiceCollectionExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void AddTokenAcquisition_AbleToOverrideICredentialsLoader()
132132

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

135-
Assert.Single(orderedServices.Where(s => s.ServiceType == typeof(ICredentialsLoader)));
135+
Assert.Single(orderedServices, s => s.ServiceType == typeof(ICredentialsLoader));
136136
}
137137

138138
[Fact]

0 commit comments

Comments
 (0)