Skip to content

Commit c2eddde

Browse files
authored
Add ResetStateForTest API for test (#5489)
* Add ResetStateForTest API for test * Keep same type * Address PR comments
1 parent 693e917 commit c2eddde

File tree

70 files changed

+220
-324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+220
-324
lines changed

src/client/Microsoft.Identity.Client/ApplicationBase.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.Identity.Client.ApiConfig.Parameters;
9+
using Microsoft.Identity.Client.AuthScheme.PoP;
10+
using Microsoft.Identity.Client.Http;
11+
using Microsoft.Identity.Client.Instance;
12+
using Microsoft.Identity.Client.Instance.Discovery;
13+
using Microsoft.Identity.Client.Instance.Oidc;
914
using Microsoft.Identity.Client.Internal;
1015
using Microsoft.Identity.Client.Internal.Requests;
16+
using Microsoft.Identity.Client.ManagedIdentity;
17+
using Microsoft.Identity.Client.OAuth2.Throttling;
18+
using Microsoft.Identity.Client.PlatformsCommon.Shared;
19+
using Microsoft.Identity.Client.Region;
1120

1221
namespace Microsoft.Identity.Client
1322
{
@@ -74,5 +83,24 @@ internal static void GuardMobileFrameworks()
7483
"See https://aka.ms/msal-net-confidential-availability and https://aka.ms/msal-net-managed-identity for details.");
7584
#endif
7685
}
86+
87+
/// <summary>
88+
/// Resets the SDKs internal state, such as static caches, to facilitate testing.
89+
/// This API is meant to be used by other SDKs that build on top of MSAL, and only by test code.
90+
/// </summary>
91+
public static void ResetStateForTest()
92+
{
93+
NetworkCacheMetadataProvider.ResetStaticCacheForTest();
94+
RegionManager.ResetStaticCacheForTest();
95+
OidcRetrieverWithCache.ResetCacheForTest();
96+
AuthorityManager.ClearValidationCache();
97+
SingletonThrottlingManager.GetInstance().ResetCache();
98+
ManagedIdentityClient.ResetSourceForTest();
99+
AuthorityManager.ClearValidationCache();
100+
PoPCryptoProviderFactory.Reset();
101+
102+
InMemoryPartitionedAppTokenCacheAccessor.ClearStaticCacheForTest();
103+
InMemoryPartitionedUserTokenCacheAccessor.ClearStaticCacheForTest();
104+
}
77105
}
78106
}

src/client/Microsoft.Identity.Client/Instance/Discovery/INetworkCacheMetadataProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ internal interface INetworkCacheMetadataProvider
99
{
1010
void AddMetadata(string environment, InstanceDiscoveryMetadataEntry entry);
1111
InstanceDiscoveryMetadataEntry GetMetadata(string environment, ILoggerAdapter logger);
12-
void /* for test purposes */ Clear();
1312
}
1413
}

src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ internal class InstanceDiscoveryManager : IInstanceDiscoveryManager
3737

3838
public InstanceDiscoveryManager(
3939
IHttpManager httpManager,
40-
bool /* for test */ shouldClearCaches,
4140
InstanceDiscoveryResponse userProvidedInstanceDiscoveryResponse = null,
4241
Uri userProvidedInstanceDiscoveryUri = null) :
4342
this(
4443
httpManager,
45-
shouldClearCaches,
4644
userProvidedInstanceDiscoveryResponse != null ? new UserMetadataProvider(userProvidedInstanceDiscoveryResponse) : null,
4745
userProvidedInstanceDiscoveryUri,
4846
null, null, null, null)
@@ -51,7 +49,6 @@ public InstanceDiscoveryManager(
5149

5250
public /* public for test */ InstanceDiscoveryManager(
5351
IHttpManager httpManager,
54-
bool shouldClearCaches,
5552
IUserMetadataProvider userMetadataProvider = null,
5653
Uri userProvidedInstanceDiscoveryUri = null,
5754
IKnownMetadataProvider knownMetadataProvider = null,
@@ -72,12 +69,9 @@ public InstanceDiscoveryManager(
7269
userProvidedInstanceDiscoveryUri);
7370

7471
_regionDiscoveryProvider = regionDiscoveryProvider ??
75-
new RegionAndMtlsDiscoveryProvider(_httpManager, shouldClearCaches);
72+
new RegionAndMtlsDiscoveryProvider(_httpManager);
7673

77-
if (shouldClearCaches)
78-
{
79-
_networkCacheMetadataProvider.Clear();
80-
}
74+
8175
}
8276

8377
public InstanceDiscoveryMetadataEntry GetMetadataEntryAvoidNetwork(

src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkCacheMetadataProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Collections.Concurrent;
56
using Microsoft.Identity.Client.Core;
67

@@ -25,7 +26,7 @@ public void AddMetadata(string environment, InstanceDiscoveryMetadataEntry entry
2526
s_cache.AddOrUpdate(environment, entry, (_, _) => entry);
2627
}
2728

28-
public void Clear()
29+
internal static void ResetStaticCacheForTest()
2930
{
3031
s_cache.Clear();
3132
}

src/client/Microsoft.Identity.Client/Instance/Discovery/RegionAndMtlsDiscoveryProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ internal class RegionAndMtlsDiscoveryProvider : IRegionDiscoveryProvider
1616
public const string PublicEnvForRegional = "login.microsoft.com";
1717
public const string PublicEnvForRegionalMtlsAuth = "mtlsauth.microsoft.com";
1818

19-
public RegionAndMtlsDiscoveryProvider(IHttpManager httpManager, bool clearCache)
19+
public RegionAndMtlsDiscoveryProvider(IHttpManager httpManager)
2020
{
21-
_regionManager = new RegionManager(httpManager, shouldClearStaticCache: clearCache);
21+
_regionManager = new RegionManager(httpManager);
2222
}
2323

2424
public async Task<InstanceDiscoveryMetadataEntry> GetMetadataAsync(Uri authority, RequestContext requestContext)
@@ -55,6 +55,7 @@ public async Task<InstanceDiscoveryMetadataEntry> GetMetadataAsync(Uri authority
5555
string regionalEnv = GetRegionalizedEnvironment(authority, region, requestContext);
5656
return CreateEntry(authority.Host, regionalEnv);
5757
}
58+
5859

5960
private static InstanceDiscoveryMetadataEntry CreateEntry(string originalEnv, string regionalEnv)
6061
{
@@ -68,7 +69,6 @@ private static InstanceDiscoveryMetadataEntry CreateEntry(string originalEnv, st
6869

6970
private static string GetRegionalizedEnvironment(Uri authority, string region, RequestContext requestContext)
7071
{
71-
7272
string host = authority.Host;
7373

7474
if (KnownMetadataProvider.IsPublicEnvironment(host))

src/client/Microsoft.Identity.Client/Instance/Region/RegionManager.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,13 @@ public RegionInfo(string region, RegionAutodetectionSource regionSource, string
4949

5050
public RegionManager(
5151
IHttpManager httpManager,
52-
int imdsCallTimeout = 2000,
53-
bool shouldClearStaticCache = false) // for test
52+
int imdsCallTimeout = 2000) // for test
5453
{
5554
_httpManager = httpManager;
5655
_imdsCallTimeoutMs = imdsCallTimeout;
57-
58-
if (shouldClearStaticCache)
59-
{
60-
s_failedAutoDiscovery = false;
61-
s_autoDiscoveredRegion = null;
62-
s_regionDiscoveryDetails = null;
63-
}
6456
}
6557

58+
6659
public async Task<string> GetAzureRegionAsync(RequestContext requestContext)
6760
{
6861
string azureRegionConfig = requestContext.ServiceBundle.Config.AzureRegion;
@@ -107,6 +100,13 @@ public async Task<string> GetAzureRegionAsync(RequestContext requestContext)
107100
return azureRegionConfig;
108101
}
109102

103+
internal static void ResetStaticCacheForTest()
104+
{
105+
s_failedAutoDiscovery = false;
106+
s_autoDiscoveredRegion = null;
107+
s_regionDiscoveryDetails = null;
108+
}
109+
110110
private static bool IsAutoDiscoveryRequested(string azureRegionConfig)
111111
{
112112
return string.Equals(azureRegionConfig, ConfidentialClientApplication.AttemptRegionDiscovery);

src/client/Microsoft.Identity.Client/Internal/ServiceBundle.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ namespace Microsoft.Identity.Client.Internal
2121
internal class ServiceBundle : IServiceBundle
2222
{
2323
internal ServiceBundle(
24-
ApplicationConfiguration config,
25-
bool shouldClearCaches = false)
24+
ApplicationConfiguration config)
2625
{
2726
Config = config;
2827

@@ -38,20 +37,13 @@ internal ServiceBundle(
3837
HttpTelemetryManager = new HttpTelemetryManager();
3938

4039
InstanceDiscoveryManager = new InstanceDiscoveryManager(
41-
HttpManager,
42-
shouldClearCaches,
40+
HttpManager,
4341
config.CustomInstanceDiscoveryMetadata,
4442
config.CustomInstanceDiscoveryMetadataUri);
4543

4644
WsTrustWebRequestManager = new WsTrustWebRequestManager(HttpManager);
4745
ThrottlingManager = SingletonThrottlingManager.GetInstance();
48-
DeviceAuthManager = config.DeviceAuthManagerForTest ?? PlatformProxy.CreateDeviceAuthManager();
49-
50-
if (shouldClearCaches) // for test
51-
{
52-
AuthorityManager.ClearValidationCache();
53-
PoPCryptoProviderFactory.Reset();
54-
}
46+
DeviceAuthManager = config.DeviceAuthManagerForTest ?? PlatformProxy.CreateDeviceAuthManager();
5547
}
5648

5749
/// <summary>

src/client/Microsoft.Identity.Client/PlatformsCommon/Shared/InMemoryPartitionedAppTokenCacheAccessor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,11 @@ private ref int GetEntryCountRef()
256256
return ref _tokenCacheAccessorOptions.UseSharedCache ? ref s_entryCount : ref _entryCount;
257257
}
258258

259+
public static void ClearStaticCacheForTest()
260+
{
261+
s_accessTokenCacheDictionary.Clear();
262+
s_appMetadataDictionary.Clear();
263+
Interlocked.Exchange(ref s_entryCount, 0);
264+
}
259265
}
260266
}

src/client/Microsoft.Identity.Client/PlatformsCommon/Shared/InMemoryPartitionedUserTokenCacheAccessor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Threading;
89
using Microsoft.Identity.Client.Cache;
910
using Microsoft.Identity.Client.Cache.Items;
1011
using Microsoft.Identity.Client.Cache.Keys;
@@ -168,7 +169,7 @@ public void DeleteAccessToken(MsalAccessTokenCacheItem item)
168169

169170
if (AccessTokenCacheDictionary.TryGetValue(partitionKey, out var partition))
170171
{
171-
bool removed = partition.TryRemove(item.CacheKey, out _);
172+
bool removed = partition.TryRemove(item.CacheKey, out _);
172173
if (removed)
173174
{
174175
System.Threading.Interlocked.Decrement(ref GetEntryCountRef());
@@ -365,5 +366,15 @@ private ref int GetEntryCountRef()
365366
{
366367
return ref _tokenCacheAccessorOptions.UseSharedCache ? ref s_entryCount : ref _entryCount;
367368
}
369+
370+
public static void ClearStaticCacheForTest()
371+
{
372+
s_accessTokenCacheDictionary.Clear();
373+
s_refreshTokenCacheDictionary.Clear();
374+
s_idTokenCacheDictionary.Clear();
375+
s_accountCacheDictionary.Clear();
376+
s_appMetadataDictionary.Clear();
377+
Interlocked.Exchange(ref s_entryCount, 0);
378+
}
368379
}
369380
}

src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ const Microsoft.Identity.Client.MsalError.InvalidCertificate = "invalid_certific
22
Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync() -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource>
33
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.ImdsV2 = 8 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
44
Microsoft.Identity.Client.ManagedIdentityApplicationBuilder.WithExtraQueryParameters(System.Collections.Generic.IDictionary<string, string> extraQueryParameters) -> Microsoft.Identity.Client.ManagedIdentityApplicationBuilder
5+
static Microsoft.Identity.Client.ApplicationBase.ResetStateForTest() -> void

0 commit comments

Comments
 (0)