Skip to content

Commit 912316a

Browse files
committed
Fix tests
1 parent 28e7cc3 commit 912316a

File tree

14 files changed

+198
-108
lines changed

14 files changed

+198
-108
lines changed

Directory.Packages.props

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
<PackageVersion Include="System.Xml.XDocument" Version="4.3.0" />
3131
<PackageVersion Include="System.Xml.XmlDocument" Version="4.3.0" />
3232
<PackageVersion Include="Xamarin.AndroidX.Browser" Version="1.4.0" PrivateAssets="All" />
33+
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>
34+
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
35+
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
36+
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
37+
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
38+
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
39+
<PackageVersion Include="System.Text.Json" Version="6.0.11" />
40+
<PackageVersion Include="System.Threading" Version="4.3.0" />
41+
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
42+
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
43+
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
44+
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
45+
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
3346
</ItemGroup>
3447
<ItemGroup>
3548
<!-- Analyzers and dev packages -->
@@ -55,6 +68,10 @@
5568
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
5669
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
5770
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
71+
<!-- Since MSAL uses the minimum possible System.Text.Json version,
72+
tests are unable to move away from Newtonsoft. For example DeepEquals method is only available in System.Text.Json 9
73+
Newtonsoft reference is preferable to bumping the version System.Text.Json in tests,
74+
to avoid changing the product behavior. -->
5875
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
5976
<PackageVersion Include="NSubstitute" Version="5.3.0" />
6077
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
@@ -67,20 +84,6 @@
6784
<PackageVersion Include="Selenium.Support" Version="4.19.0" />
6885
<PackageVersion Include="Selenium.WebDriver" Version="4.19.0" />
6986
<PackageVersion Include="StrongNamer" Version="0.2.5" />
70-
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
71-
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
72-
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
73-
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
74-
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
75-
<PackageVersion Include="System.Text.Json" Version="6.0.10" />
76-
<PackageVersion Include="System.Threading" Version="4.3.0" />
77-
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
78-
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
79-
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
80-
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
81-
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
8287
<PackageVersion Include="CommandLineParser" Version="2.8.0" />
83-
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>
84-
8588
</ItemGroup>
8689
</Project>

tests/CacheCompat/CommonCache.Test.Unit/CacheExecutionTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ private static async Task<LabUserData> GetPublicAadUserDataAsync()
3131
[AssemblyInitialize]
3232
public static void AssemblyInit(TestContext testContext)
3333
{
34-
// TODO: add other users to the mix
3534
s_labUsers.Add(GetPublicAadUserDataAsync().GetAwaiter().GetResult());
3635
}
3736

tests/CacheCompat/CommonCache.Test.Unit/Utils/CacheProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using CommonCache.Test.Common;
13-
using Microsoft.Identity.Json;
13+
using Newtonsoft.Json;
1414

1515
namespace CommonCache.Test.Unit.Utils
1616
{

tests/CacheCompat/CommonCache.Test.Unit/Utils/CacheProgramResults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.IO;
55
using CommonCache.Test.Common;
6-
using Microsoft.Identity.Json;
6+
using Newtonsoft.Json;
77

88
namespace CommonCache.Test.Unit.Utils
99
{

tests/Microsoft.Identity.Test.Common/Core/Mocks/MockHelpers.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,10 @@ public static string GetMsiSuccessfulResponse(int expiresInHours = 1, bool useIs
133133
"\"Bearer\",\"client_id\":\"client_id\"}";
134134
}
135135

136-
public static string GetMsiImdsSuccessfulResponse()
136+
public static string GetMsiErrorBadJson()
137137
{
138-
string expiresOn = DateTimeHelpers.DateTimeToUnixTimestamp(DateTime.UtcNow.AddHours(1));
139-
return
140-
"{\"access_token\":\"" + TestConstants.ATSecret + "\",\"client_id\":\"client-id\"," +
141-
"\"expires_in\":\"12345\",\"expires_on\":\"" + expiresOn + "\",\"resource\":\"https://management.azure.com/\"," +
142-
"\"ext_expires_in\":\"12345\",\"token_type\":\"Bearer\"}";
138+
string successResponse = GetMsiSuccessfulResponse();
139+
return successResponse.Replace("{", "|");
143140
}
144141

145142
public static string GetMsiErrorResponse(ManagedIdentitySource source)

tests/Microsoft.Identity.Test.Integration.netcore/HeadlessTests/ManagedIdentityTests.NetFwk.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Microsoft.Identity.Client.AuthScheme;
1717
using Microsoft.Identity.Client.Http;
1818
using Microsoft.Identity.Client.ManagedIdentity;
19-
2019
using Microsoft.Identity.Test.Common.Core.Helpers;
2120
using Microsoft.Identity.Test.Integration.NetFx.Infrastructure;
2221
using Microsoft.Identity.Test.LabInfrastructure;
@@ -392,13 +391,8 @@ private async Task<Dictionary<string, string>> GetEnvironmentVariablesAsync(
392391
//process the response
393392
if (!string.IsNullOrEmpty(environmentVariableResponse))
394393
{
395-
#if NET8_0_OR_GREATER
396394
environmentVariables = System.Text.Json.JsonSerializer.Deserialize
397395
<Dictionary<string, string>>(environmentVariableResponse);
398-
#else
399-
environmentVariables = Microsoft.Identity.Json.JsonConvert.DeserializeObject
400-
<Dictionary<string, string>>(environmentVariableResponse);
401-
#endif
402396
}
403397

404398
return environmentVariables;

tests/Microsoft.Identity.Test.Unit/CacheTests/CacheSerializationTests.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@
1414
using Microsoft.Identity.Client.Internal;
1515
using Microsoft.Identity.Client.PlatformsCommon.Shared;
1616
using Microsoft.Identity.Client.Utils;
17-
#if NET8_0_OR_GREATER
1817
using System.Text.Json;
1918
using System.Text.Json.Nodes;
2019
using JObject = System.Text.Json.Nodes.JsonObject;
21-
#else
22-
using Microsoft.Identity.Json.Linq;
23-
#endif
2420
using Microsoft.Identity.Test.Common;
2521
using Microsoft.Identity.Test.Common.Core.Helpers;
2622
using Microsoft.Identity.Test.Common.Core.Mocks;
@@ -45,12 +41,10 @@ public void TestInitialize()
4541
StorageJsonKeys.FamilyId
4642
};
4743

48-
#if NET8_0_OR_GREATER
4944
private static readonly JsonDocumentOptions _documentOptions = new JsonDocumentOptions
5045
{
5146
AllowTrailingCommas = true
5247
};
53-
#endif
5448

5549
private MsalAccessTokenCacheItem CreateAccessTokenItem(
5650
string kid = null,
@@ -264,7 +258,7 @@ public void TestSerializeMsalAccessTokenCacheItem_WithRefreshOn()
264258
string asJson = item.ToJsonString();
265259
var item2 = MsalAccessTokenCacheItem.FromJsonString(asJson);
266260

267-
AssertAccessTokenCacheItemsAreEqual(item, item2, "123"); // TODO: fix this
261+
AssertAccessTokenCacheItemsAreEqual(item, item2, "123");
268262
}
269263

270264
[TestMethod]
@@ -556,17 +550,11 @@ public async Task UnknownNodesTestAsync()
556550
await (tokenCache as ITokenCacheInternal).OnAfterAccessAsync(notification).ConfigureAwait(false);
557551
(tokenCache as ITokenCacheInternal).Accessor.AssertItemCount(5, 4, 3, 3, 3);
558552

559-
#if NET8_0_OR_GREATER
560553
var finalJson = JsonNode.Parse(Encoding.UTF8.GetString(cache), documentOptions: _documentOptions).AsObject();
561554

562555
var originalJson = JsonNode.Parse(jsonContent, documentOptions: _documentOptions).AsObject();
563556
Assert.IsTrue(DeepEquals(originalJson, finalJson));
564-
#else
565-
var finalJson = JObject.Parse(Encoding.UTF8.GetString(cache));
566557

567-
var originalJson = JObject.Parse(jsonContent);
568-
Assert.IsTrue(JToken.DeepEquals(originalJson, finalJson));
569-
#endif
570558
}
571559

572560
[TestMethod]
@@ -580,13 +568,10 @@ public void TestJsonSerialization()
580568
byte[] bytes = s1.Serialize(null);
581569
string actualJson = new UTF8Encoding().GetString(bytes);
582570

583-
#if NET8_0_OR_GREATER
584571
Assert.IsTrue(DeepEquals(
585572
JsonNode.Parse(actualJson, documentOptions: _documentOptions).AsObject(),
586573
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
587-
#else
588-
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson), JObject.Parse(expectedJson)));
589-
#endif
574+
590575
var otherAccessor = new InMemoryPartitionedUserTokenCacheAccessor(Substitute.For<ILoggerAdapter>(), null);
591576
var s2 = new TokenCacheJsonSerializer(otherAccessor);
592577
s2.Deserialize(bytes, false);
@@ -596,13 +581,11 @@ public void TestJsonSerialization()
596581
// serialize again to detect errors that come from deserialization
597582
byte[] bytes2 = s2.Serialize(null);
598583
string actualJson2 = new UTF8Encoding().GetString(bytes2);
599-
#if NET8_0_OR_GREATER
584+
600585
Assert.IsTrue(DeepEquals(
601586
JsonNode.Parse(actualJson2, documentOptions: _documentOptions).AsObject(),
602587
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
603-
#else
604-
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
605-
#endif
588+
606589
}
607590

608591
[TestMethod]
@@ -656,13 +639,11 @@ public void TestDeserializeWithClearCache()
656639
// serialize again to detect errors that come from deserialization
657640
byte[] bytes2 = s1.Serialize(null);
658641
string actualJson2 = new UTF8Encoding().GetString(bytes2);
659-
#if NET8_0_OR_GREATER
642+
660643
Assert.IsTrue(DeepEquals(
661644
JsonNode.Parse(actualJson2, documentOptions: _documentOptions).AsObject(),
662645
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
663-
#else
664-
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
665-
#endif
646+
666647
}
667648

668649
[TestMethod]
@@ -958,7 +939,6 @@ private void AssertAccountCacheItemsAreEqual(MsalAccountCacheItem expected, Msal
958939
CoreAssert.AssertDictionariesAreEqual(expected.WamAccountIds, actual.WamAccountIds, StringComparer.Ordinal);
959940
}
960941

961-
#if NET8_0_OR_GREATER
962942
private bool DeepEquals(JsonNode a, JsonNode b)
963943
{
964944
if (a == null && b == null)
@@ -1016,6 +996,5 @@ private bool ObjectEquals(JsonObject obj1, JsonObject obj2)
1016996
}
1017997
return true;
1018998
}
1019-
#endif
1020999
}
10211000
}

tests/Microsoft.Identity.Test.Unit/CoreTests/WsTrustTests/MexParserTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public void WsTrust2005AddressExtractionTest()
4949
[Description("Mex endpoint fails to resolve")]
5050
public async Task MexEndpointFailsToResolveTestAsync()
5151
{
52-
// TODO: should we move this into a separate test class for WsTrustWebRequestManager?
5352
using (var harness = CreateTestHarness())
5453
{
5554
harness.HttpManager.AddMockHandlerContentNotFound(HttpMethod.Get);

tests/Microsoft.Identity.Test.Unit/Kerberos/KerberosSupplementalTicketManagerTests.cs

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Licensed under the MIT License.
33

44
#if NETFRAMEWORK
5-
using System.Linq;
65
using Microsoft.Identity.Client.Kerberos;
7-
using Microsoft.Identity.Client.Utils;
8-
using Microsoft.Identity.Json.Linq;
96
using Microsoft.Identity.Test.Common;
107
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using System.Text.Json;
119

1210
namespace Microsoft.Identity.Test.Unit.Kerberos
1311
{
@@ -22,17 +20,17 @@ public class KerberosSupplementalTicketManagerTests
2220
/// <summary>
2321
/// Service principal name for testing.
2422
/// </summary>
25-
private static readonly string _testServicePrincipalName = "HTTP/prod.aadkreberos.msal.com";
23+
private const string TestServicePrincipalName = "HTTP/prod.aadkreberos.msal.com";
2624

2725
/// <summary>
2826
/// Username within the ID token.
2927
/// </summary>
30-
private static readonly string _testClientName = "localAdmin@aadktest.onmicrosoft.com";
28+
private const string TestClientName = "localAdmin@aadktest.onmicrosoft.com";
3129

3230
/// <summary>
3331
/// Sample ID Token without Kerbero Service Ticket.
3432
/// </summary>
35-
private static readonly string _testIdToken =
33+
private const string TestIdToken =
3634
"eyJ0eXAiOiJKV1QiLCJyaCI6IjAuQWdBQXI0R0lRckdhczBDQldEWVJOWV9fYUlLMElWSlJKck5NbXRqQW1uamszcDRzQU5NLiIsImFsZyI6IlJTMjU2"
3735
+ "Iiwia2lkIjoibk9vM1pEck9EWEVLMWpLV2hYc2xIUl9LWEVnIn0.eyJhdWQiOiI1MjIxYjQ4Mi0yNjUxLTRjYjMtOWFkOC1jMDlhNzhlNGRlOWUiLCJp"
3836
+ "c3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNDI4ODgxYWYtOWFiMS00MGIzLTgxNTgtMzYxMTM1OGZmZjY4L3YyLjAiLCJpYXQi"
@@ -48,7 +46,7 @@ public class KerberosSupplementalTicketManagerTests
4846
/// <summary>
4947
/// Sample ID token sample with Kerberos Service Ticket.
5048
/// </summary>
51-
private static readonly string _testIdTokenWithKerberosTicketClaim =
49+
private const string TestIdTokenWithKerberosTicketClaim =
5250
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiI1MjIxYjQ4Mi0yNjUxLTRj"
5351
+ "YjMtOWFkOC1jMDlhNzhlNGRlOWUiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNDI4ODgxYWYtOWFiMS00MGIzLTgxNTgtMz"
5452
+ "YxMTM1OGZmZjY4L3YyLjAiLCJpYXQiOjE2MTk4MTg1MTgsIm5iZiI6MTYxOTgxODUxOCwiZXhwIjoxNjE5ODIyNDE4LCJhaW8iOiJBVFFBeS84VEFBQUFP"
@@ -99,27 +97,27 @@ public void TestInit()
9997
[TestMethod]
10098
public void FromIdToken_WithKerberosTicket()
10199
{
102-
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdTokenWithKerberosTicketClaim);
100+
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdTokenWithKerberosTicketClaim);
103101

104102
Assert.IsNotNull(ticket);
105103
Assert.IsTrue(string.IsNullOrEmpty(ticket.ErrorMessage));
106104
Assert.IsFalse(string.IsNullOrEmpty(ticket.KerberosMessageBuffer));
107-
Assert.AreEqual(_testServicePrincipalName, ticket.ServicePrincipalName, "Service principal name is not matched.");
108-
Assert.AreEqual(_testClientName, ticket.ClientName, "Client name is not matched.");
105+
Assert.AreEqual(TestServicePrincipalName, ticket.ServicePrincipalName, "Service principal name is not matched.");
106+
Assert.AreEqual(TestClientName, ticket.ClientName, "Client name is not matched.");
109107
}
110108

111109
[TestMethod]
112110
public void FromIdToken_WithoutKerberosTicket()
113111
{
114-
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdToken);
112+
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdToken);
115113

116114
Assert.IsNull(ticket);
117115
}
118116

119117
[TestMethod]
120118
public void GetKrbCred()
121119
{
122-
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdTokenWithKerberosTicketClaim);
120+
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdTokenWithKerberosTicketClaim);
123121
byte[] krbCred = KerberosSupplementalTicketManager.GetKrbCred(ticket);
124122

125123
Assert.IsNotNull(krbCred);
@@ -128,53 +126,41 @@ public void GetKrbCred()
128126
[TestMethod]
129127
public void GetKerberosTicketClaim_IdToken()
130128
{
129+
// {id_token: { "xms_as_rep":{"essential":"false","value":"HTTP/prod.aadkreberos.msal.com"} } }
131130
string kerberosClaim
132-
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(_testServicePrincipalName, KerberosTicketContainer.IdToken);
133-
134-
Assert.IsFalse(string.IsNullOrEmpty(kerberosClaim));
135-
JsonHelper.DeserializeFromJson<JObject>(kerberosClaim);
136-
137-
JObject claim = JObject.Parse(kerberosClaim);
138-
Assert.IsNotNull(claim);
139-
140-
Assert.IsTrue(claim.ContainsKey("id_token"));
141-
JToken idToken = claim.GetValue("id_token");
142-
143-
144-
Assert.IsNotNull(idToken);
145-
146-
147-
CheckKerberosClaim(idToken);
131+
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(TestServicePrincipalName, KerberosTicketContainer.IdToken);
132+
133+
using (JsonDocument document = JsonDocument.Parse(kerberosClaim))
134+
{
135+
JsonElement root = document.RootElement;
136+
137+
Assert.IsTrue(root.TryGetProperty("id_token", out JsonElement idToken), "id_token property is missing.");
138+
CheckKerberosClaimContent(idToken);
139+
}
148140
}
149141

150142
[TestMethod]
151143
public void GetKerberosTicketClaim_AccessToken()
152144
{
153145
string kerberosClaim
154-
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(_testServicePrincipalName, KerberosTicketContainer.AccessToken);
155-
156-
Assert.IsFalse(string.IsNullOrEmpty(kerberosClaim));
146+
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(TestServicePrincipalName, KerberosTicketContainer.AccessToken);
157147

158-
JObject claim = JObject.Parse(kerberosClaim);
159-
Assert.IsNotNull(claim);
148+
using (JsonDocument document = JsonDocument.Parse(kerberosClaim))
149+
{
150+
JsonElement root = document.RootElement;
160151

161-
Assert.IsTrue(claim.ContainsKey("access_token"));
162-
JToken accessToken = claim.GetValue("access_token");
163-
Assert.IsNotNull(accessToken);
164-
165-
CheckKerberosClaim(accessToken);
152+
Assert.IsTrue(root.TryGetProperty("access_token", out JsonElement accessToken), "access_token property is missing.");
153+
CheckKerberosClaimContent(accessToken);
154+
}
166155
}
167156

168-
private void CheckKerberosClaim(JToken claim)
169-
{
170-
JToken asRep = claim["xms_as_rep"];
171-
Assert.IsNotNull(asRep);
172-
173-
Assert.AreEqual("false", asRep["essential"].Value<string>(),
174-
"essential field is not matched.");
175-
176-
Assert.AreEqual(_testServicePrincipalName, asRep["value"].Value<string>(),
177-
"Service principal name is not matched.");
157+
private static void CheckKerberosClaimContent(JsonElement idToken)
158+
{
159+
Assert.IsTrue(idToken.TryGetProperty("xms_as_rep", out JsonElement xmsAsRep), "xms_as_rep property is missing.");
160+
Assert.IsTrue(xmsAsRep.TryGetProperty("essential", out JsonElement essential), "essential property is missing.");
161+
Assert.AreEqual("false", essential.GetString(), "essential value is not matched.");
162+
Assert.IsTrue(xmsAsRep.TryGetProperty("value", out JsonElement value), "value property is missing.");
163+
Assert.AreEqual(TestServicePrincipalName, value.GetString(), "value is not matched.");
178164
}
179165
}
180166
}

0 commit comments

Comments
 (0)