Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Add test for IDN cert validation
  • Loading branch information
rzikm committed Apr 21, 2023
commit 98b827b31a2751ad63e52c1ef4afccdbd184f417
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Net.Test.Common;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
Expand Down Expand Up @@ -235,6 +236,49 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
}
}

[Fact]
public async Task UnencodedHostName_ValidatesCertificate()
{
string rawHostname = "räksmörgås.josefsson.org";
string punycodeHostname = "xn--rksmrgs-5wao1o.josefsson.org";

var (serverCert, serverChain) = TestHelper.GenerateCertificates(punycodeHostname);
try
{
SslServerAuthenticationOptions serverOptions = new SslServerAuthenticationOptions()
{
ServerCertificateContext = SslStreamCertificateContext.Create(serverCert, serverChain),
};

SslClientAuthenticationOptions clientOptions = new ()
{
TargetHost = rawHostname,
CertificateChainPolicy = new X509ChainPolicy()
{
RevocationMode = X509RevocationMode.NoCheck,
TrustMode = X509ChainTrustMode.CustomRootTrust,
CustomTrustStore = { serverChain[serverChain.Count - 1] }
}
};

(SslStream client, SslStream server) = TestHelper.GetConnectedSslStreams();

await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
client.AuthenticateAsClientAsync(clientOptions, default),
server.AuthenticateAsServerAsync(serverOptions, default));

await TestHelper.PingPong(client, server, default);
Assert.Equal(rawHostname, server.TargetHostName);
Assert.Equal(rawHostname, client.TargetHostName);
}
finally
{
serverCert.Dispose();
foreach (var c in serverChain) c.Dispose();
TestHelper.CleanupCertificates(rawHostname);
}
}

[Theory]
[InlineData("www-.volal.cz")]
[InlineData("www-.colorhexa.com")]
Expand Down Expand Up @@ -263,6 +307,7 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout(

await TestHelper.PingPong(client, server, default);
Assert.Equal(name, server.TargetHostName);
Assert.Equal(name, client.TargetHostName);
}
}

Expand Down