Move TLS, OCSP, WebSocket, Networking, and IO test files from NATS.Server.Tests into a dedicated NATS.Server.Transport.Tests project. Update namespaces, replace private GetFreePort/ReadUntilAsync with shared TestUtilities helpers, extract TestCertHelper to TestUtilities, and replace Task.Delay polling loops with PollHelper.WaitUntilAsync/YieldForAsync for proper synchronization.
32 lines
1.2 KiB
C#
32 lines
1.2 KiB
C#
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace NATS.Server.TestUtilities;
|
|
|
|
public static class TestCertHelper
|
|
{
|
|
public static (string certPath, string keyPath) GenerateTestCertFiles()
|
|
{
|
|
var (cert, key) = GenerateTestCert();
|
|
var certPath = Path.GetTempFileName();
|
|
var keyPath = Path.GetTempFileName();
|
|
File.WriteAllText(certPath, cert.ExportCertificatePem());
|
|
File.WriteAllText(keyPath, key.ExportPkcs8PrivateKeyPem());
|
|
return (certPath, keyPath);
|
|
}
|
|
|
|
public static (X509Certificate2 cert, RSA key) GenerateTestCert()
|
|
{
|
|
var key = RSA.Create(2048);
|
|
var req = new CertificateRequest("CN=localhost", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
req.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
|
|
var sanBuilder = new SubjectAlternativeNameBuilder();
|
|
sanBuilder.AddIpAddress(IPAddress.Loopback);
|
|
sanBuilder.AddDnsName("localhost");
|
|
req.CertificateExtensions.Add(sanBuilder.Build());
|
|
var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(1));
|
|
return (cert, key);
|
|
}
|
|
}
|