Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Jan 18, 2025
1 parent dd4d0c9 commit c90eb62
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 102 deletions.
31 changes: 17 additions & 14 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
<PackageVersion Include="System.Xml.XDocument" Version="4.3.0" />
<PackageVersion Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageVersion Include="Xamarin.AndroidX.Browser" Version="1.4.0" PrivateAssets="All" />
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.11" />
<PackageVersion Include="System.Threading" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<!-- Analyzers and dev packages -->
Expand All @@ -55,6 +68,10 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
<!-- Since MSAL uses the minimum possible System.Text.Json version,
tests are unable to move away from Newtonsoft. For example DeepEquals method is only available in System.Text.Json 9
Newtonsoft reference is preferable to bumping the version System.Text.Json in tests,
to avoid changing the product behavior. -->
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
Expand All @@ -67,20 +84,6 @@
<PackageVersion Include="Selenium.Support" Version="4.19.0" />
<PackageVersion Include="Selenium.WebDriver" Version="4.19.0" />
<PackageVersion Include="StrongNamer" Version="0.2.5" />
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.10" />
<PackageVersion Include="System.Threading" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
<PackageVersion Include="CommandLineParser" Version="2.8.0" />
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private static async Task<LabUserData> GetPublicAadUserDataAsync()
[AssemblyInitialize]
public static void AssemblyInit(TestContext testContext)
{
// TODO: add other users to the mix
s_labUsers.Add(GetPublicAadUserDataAsync().GetAwaiter().GetResult());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using CommonCache.Test.Common;
using Microsoft.Identity.Json;
using Newtonsoft.Json;

namespace CommonCache.Test.Unit.Utils
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.IO;
using CommonCache.Test.Common;
using Microsoft.Identity.Json;
using Newtonsoft.Json;

namespace CommonCache.Test.Unit.Utils
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Microsoft.Identity.Client.AuthScheme;
using Microsoft.Identity.Client.Http;
using Microsoft.Identity.Client.ManagedIdentity;

using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.Identity.Test.Integration.NetFx.Infrastructure;
using Microsoft.Identity.Test.LabInfrastructure;
Expand Down Expand Up @@ -392,13 +391,8 @@ private async Task<Dictionary<string, string>> GetEnvironmentVariablesAsync(
//process the response
if (!string.IsNullOrEmpty(environmentVariableResponse))
{
#if NET8_0_OR_GREATER
environmentVariables = System.Text.Json.JsonSerializer.Deserialize
<Dictionary<string, string>>(environmentVariableResponse);
#else
environmentVariables = Microsoft.Identity.Json.JsonConvert.DeserializeObject
<Dictionary<string, string>>(environmentVariableResponse);
#endif
}

return environmentVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.PlatformsCommon.Shared;
using Microsoft.Identity.Client.Utils;
#if NET8_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Nodes;
using JObject = System.Text.Json.Nodes.JsonObject;
#else
using Microsoft.Identity.Json.Linq;
#endif
using Microsoft.Identity.Test.Common;
using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.Identity.Test.Common.Core.Mocks;
Expand All @@ -45,12 +41,10 @@ public void TestInitialize()
StorageJsonKeys.FamilyId
};

#if NET8_0_OR_GREATER
private static readonly JsonDocumentOptions _documentOptions = new JsonDocumentOptions
{
AllowTrailingCommas = true
};
#endif

private MsalAccessTokenCacheItem CreateAccessTokenItem(
string kid = null,
Expand Down Expand Up @@ -264,7 +258,7 @@ public void TestSerializeMsalAccessTokenCacheItem_WithRefreshOn()
string asJson = item.ToJsonString();
var item2 = MsalAccessTokenCacheItem.FromJsonString(asJson);

AssertAccessTokenCacheItemsAreEqual(item, item2, "123"); // TODO: fix this
AssertAccessTokenCacheItemsAreEqual(item, item2, "123");
}

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

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

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

var originalJson = JObject.Parse(jsonContent);
Assert.IsTrue(JToken.DeepEquals(originalJson, finalJson));
#endif
}

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

#if NET8_0_OR_GREATER
Assert.IsTrue(DeepEquals(
JsonNode.Parse(actualJson, documentOptions: _documentOptions).AsObject(),
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
#else
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson), JObject.Parse(expectedJson)));
#endif

var otherAccessor = new InMemoryPartitionedUserTokenCacheAccessor(Substitute.For<ILoggerAdapter>(), null);
var s2 = new TokenCacheJsonSerializer(otherAccessor);
s2.Deserialize(bytes, false);
Expand All @@ -596,13 +581,11 @@ public void TestJsonSerialization()
// serialize again to detect errors that come from deserialization
byte[] bytes2 = s2.Serialize(null);
string actualJson2 = new UTF8Encoding().GetString(bytes2);
#if NET8_0_OR_GREATER

Assert.IsTrue(DeepEquals(
JsonNode.Parse(actualJson2, documentOptions: _documentOptions).AsObject(),
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
#else
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
#endif

}

[TestMethod]
Expand Down Expand Up @@ -656,13 +639,11 @@ public void TestDeserializeWithClearCache()
// serialize again to detect errors that come from deserialization
byte[] bytes2 = s1.Serialize(null);
string actualJson2 = new UTF8Encoding().GetString(bytes2);
#if NET8_0_OR_GREATER

Assert.IsTrue(DeepEquals(
JsonNode.Parse(actualJson2, documentOptions: _documentOptions).AsObject(),
JsonNode.Parse(expectedJson, documentOptions: _documentOptions).AsObject()));
#else
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
#endif

}

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

#if NET8_0_OR_GREATER
private bool DeepEquals(JsonNode a, JsonNode b)
{
if (a == null && b == null)
Expand Down Expand Up @@ -1016,6 +996,5 @@ private bool ObjectEquals(JsonObject obj1, JsonObject obj2)
}
return true;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void WsTrust2005AddressExtractionTest()
[Description("Mex endpoint fails to resolve")]
public async Task MexEndpointFailsToResolveTestAsync()
{
// TODO: should we move this into a separate test class for WsTrustWebRequestManager?
using (var harness = CreateTestHarness())
{
harness.HttpManager.AddMockHandlerContentNotFound(HttpMethod.Get);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Licensed under the MIT License.

#if NETFRAMEWORK
using System.Linq;
using Microsoft.Identity.Client.Kerberos;
using Microsoft.Identity.Client.Utils;
using Microsoft.Identity.Json.Linq;
using Microsoft.Identity.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.Json;

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

/// <summary>
/// Username within the ID token.
/// </summary>
private static readonly string _testClientName = "[email protected]";
private const string TestClientName = "[email protected]";

/// <summary>
/// Sample ID Token without Kerbero Service Ticket.
/// </summary>
private static readonly string _testIdToken =
private const string TestIdToken =
"eyJ0eXAiOiJKV1QiLCJyaCI6IjAuQWdBQXI0R0lRckdhczBDQldEWVJOWV9fYUlLMElWSlJKck5NbXRqQW1uamszcDRzQU5NLiIsImFsZyI6IlJTMjU2"
+ "Iiwia2lkIjoibk9vM1pEck9EWEVLMWpLV2hYc2xIUl9LWEVnIn0.eyJhdWQiOiI1MjIxYjQ4Mi0yNjUxLTRjYjMtOWFkOC1jMDlhNzhlNGRlOWUiLCJp"
+ "c3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNDI4ODgxYWYtOWFiMS00MGIzLTgxNTgtMzYxMTM1OGZmZjY4L3YyLjAiLCJpYXQi"
Expand All @@ -48,7 +46,7 @@ public class KerberosSupplementalTicketManagerTests
/// <summary>
/// Sample ID token sample with Kerberos Service Ticket.
/// </summary>
private static readonly string _testIdTokenWithKerberosTicketClaim =
private const string TestIdTokenWithKerberosTicketClaim =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiI1MjIxYjQ4Mi0yNjUxLTRj"
+ "YjMtOWFkOC1jMDlhNzhlNGRlOWUiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNDI4ODgxYWYtOWFiMS00MGIzLTgxNTgtMz"
+ "YxMTM1OGZmZjY4L3YyLjAiLCJpYXQiOjE2MTk4MTg1MTgsIm5iZiI6MTYxOTgxODUxOCwiZXhwIjoxNjE5ODIyNDE4LCJhaW8iOiJBVFFBeS84VEFBQUFP"
Expand Down Expand Up @@ -99,27 +97,27 @@ public void TestInit()
[TestMethod]
public void FromIdToken_WithKerberosTicket()
{
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdTokenWithKerberosTicketClaim);
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdTokenWithKerberosTicketClaim);

Assert.IsNotNull(ticket);
Assert.IsTrue(string.IsNullOrEmpty(ticket.ErrorMessage));
Assert.IsFalse(string.IsNullOrEmpty(ticket.KerberosMessageBuffer));
Assert.AreEqual(_testServicePrincipalName, ticket.ServicePrincipalName, "Service principal name is not matched.");
Assert.AreEqual(_testClientName, ticket.ClientName, "Client name is not matched.");
Assert.AreEqual(TestServicePrincipalName, ticket.ServicePrincipalName, "Service principal name is not matched.");
Assert.AreEqual(TestClientName, ticket.ClientName, "Client name is not matched.");
}

[TestMethod]
public void FromIdToken_WithoutKerberosTicket()
{
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdToken);
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdToken);

Assert.IsNull(ticket);
}

[TestMethod]
public void GetKrbCred()
{
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(_testIdTokenWithKerberosTicketClaim);
KerberosSupplementalTicket ticket = KerberosSupplementalTicketManager.FromIdToken(TestIdTokenWithKerberosTicketClaim);
byte[] krbCred = KerberosSupplementalTicketManager.GetKrbCred(ticket);

Assert.IsNotNull(krbCred);
Expand All @@ -128,53 +126,41 @@ public void GetKrbCred()
[TestMethod]
public void GetKerberosTicketClaim_IdToken()
{
// {id_token: { "xms_as_rep":{"essential":"false","value":"HTTP/prod.aadkreberos.msal.com"} } }
string kerberosClaim
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(_testServicePrincipalName, KerberosTicketContainer.IdToken);

Assert.IsFalse(string.IsNullOrEmpty(kerberosClaim));
JsonHelper.DeserializeFromJson<JObject>(kerberosClaim);

JObject claim = JObject.Parse(kerberosClaim);
Assert.IsNotNull(claim);

Assert.IsTrue(claim.ContainsKey("id_token"));
JToken idToken = claim.GetValue("id_token");


Assert.IsNotNull(idToken);


CheckKerberosClaim(idToken);
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(TestServicePrincipalName, KerberosTicketContainer.IdToken);

using (JsonDocument document = JsonDocument.Parse(kerberosClaim))
{
JsonElement root = document.RootElement;

Assert.IsTrue(root.TryGetProperty("id_token", out JsonElement idToken), "id_token property is missing.");
CheckKerberosClaimContent(idToken);
}
}

[TestMethod]
public void GetKerberosTicketClaim_AccessToken()
{
string kerberosClaim
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(_testServicePrincipalName, KerberosTicketContainer.AccessToken);

Assert.IsFalse(string.IsNullOrEmpty(kerberosClaim));
= KerberosSupplementalTicketManager.GetKerberosTicketClaim(TestServicePrincipalName, KerberosTicketContainer.AccessToken);

JObject claim = JObject.Parse(kerberosClaim);
Assert.IsNotNull(claim);
using (JsonDocument document = JsonDocument.Parse(kerberosClaim))
{
JsonElement root = document.RootElement;

Assert.IsTrue(claim.ContainsKey("access_token"));
JToken accessToken = claim.GetValue("access_token");
Assert.IsNotNull(accessToken);

CheckKerberosClaim(accessToken);
Assert.IsTrue(root.TryGetProperty("access_token", out JsonElement accessToken), "access_token property is missing.");
CheckKerberosClaimContent(accessToken);
}
}

private void CheckKerberosClaim(JToken claim)
{
JToken asRep = claim["xms_as_rep"];
Assert.IsNotNull(asRep);

Assert.AreEqual("false", asRep["essential"].Value<string>(),
"essential field is not matched.");

Assert.AreEqual(_testServicePrincipalName, asRep["value"].Value<string>(),
"Service principal name is not matched.");
private static void CheckKerberosClaimContent(JsonElement idToken)
{
Assert.IsTrue(idToken.TryGetProperty("xms_as_rep", out JsonElement xmsAsRep), "xms_as_rep property is missing.");
Assert.IsTrue(xmsAsRep.TryGetProperty("essential", out JsonElement essential), "essential property is missing.");
Assert.AreEqual("false", essential.GetString(), "essential value is not matched.");
Assert.IsTrue(xmsAsRep.TryGetProperty("value", out JsonElement value), "value property is missing.");
Assert.AreEqual(TestServicePrincipalName, value.GetString(), "value is not matched.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

<ItemGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetDesktop48)'">
<PackageReference Include="System.Windows.Forms" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c90eb62

Please sign in to comment.