Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
segor committed Feb 16, 2024
1 parent a0e1b92 commit 6eeb048
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void QueryOnLinuxIsNotSupported()
public void DeleteOnLinuxIsNotSupported()
{
var config = new CertificateBindingConfiguration();
var endPoint = new IPEndPoint(1, 1).ToDnsEndPoint();
var endPoint = new IPEndPoint(1, 1).ToBindingEndPoint();
PlatformNotSupportedException ex = Assert.Throws<PlatformNotSupportedException>(() => config.Delete(endPoint));
Assert.That(ex, Has.InnerException.TypeOf<DllNotFoundException>());
}
Expand Down
4 changes: 2 additions & 2 deletions src/SslCertBinding.Net.Tests/CertificateBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CertificateBindingTests
[Test]
public void ConstructorWithEmptyCertificateThumbprintShouldFailTest()
{
void constructor() => _ = new CertificateBinding(string.Empty, "MY", new IPEndPoint(0, 0).ToBindingEndPoint(), Guid.Empty);
void constructor() => _ = new CertificateBinding(string.Empty, "MY", new IPEndPoint(0, 0).ToDnsEndPoint(), Guid.Empty);

ArgumentException ex = Assert.Throws<ArgumentException>(constructor);
Assert.Multiple(() =>
Expand All @@ -29,7 +29,7 @@ public void ConstructorWithNullIpportShouldFailTest()
Assert.Multiple(() =>
{
Assert.That(ex.Message, Does.StartWith("Value cannot be null."));
Assert.That(ex.ParamName, Is.EqualTo("ipPort"));
Assert.That(ex.ParamName, Is.EqualTo("endPoint"));
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/SslCertBinding.Net/ArgumentValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace SslCertBinding.Net
{
internal static class ArgumentValidation
{
public static T ThrowIfNull<T>(this T arg, string paramName) where T : class
{
return arg is null ? throw new ArgumentNullException(paramName) : arg;
}
}
}
35 changes: 13 additions & 22 deletions src/SslCertBinding.Net/BindingEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,32 @@ public class BindingEndPoint : DnsEndPoint
public bool IsIpEndpoint => _ipEndPoint != null;
public IPAddress IPAddress => _ipEndPoint?.Address;

public BindingEndPoint(string host, int port)
: this(host, port, TryGetIPAddressFromHostArgument(host))
public BindingEndPoint(string host, int port) : this(
host, port,
TryGetIPAddressFromHost(host.ThrowIfNull(nameof(host))))

Check warning on line 17 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L15-L17

Added lines #L15 - L17 were not covered by tests
{
}

Check warning on line 19 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L19

Added line #L19 was not covered by tests

public BindingEndPoint(IPAddress ipAddress, int port)
: this(ipAddress.ToString(), port, ipAddress)
public BindingEndPoint(IPAddress ipAddress, int port): this(
ipAddress.ThrowIfNull(nameof(ipAddress)).ToString(),
port, ipAddress)
{
if (ipAddress is null)
{
throw new ArgumentNullException(nameof(ipAddress));
}
}

public BindingEndPoint(IPEndPoint ipEndPoint)
: this(ipEndPoint.Address, ipEndPoint.Port)
public BindingEndPoint(IPEndPoint ipEndPoint) : this(
ipEndPoint.ThrowIfNull(nameof(ipEndPoint)).Address,
ipEndPoint.Port)
{
if (ipEndPoint is null)
{
throw new ArgumentNullException(nameof(ipEndPoint));

Check warning on line 33 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L33

Added line #L33 was not covered by tests
}
}

public BindingEndPoint(DnsEndPoint dnsEndPoint)
: this(dnsEndPoint.Host, dnsEndPoint.Port)
public BindingEndPoint(DnsEndPoint dnsEndPoint) : this(
dnsEndPoint.ThrowIfNull(nameof(dnsEndPoint)).Host,
dnsEndPoint.Port)

Check warning on line 39 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L37-L39

Added lines #L37 - L39 were not covered by tests
{
if (dnsEndPoint is null)
{
throw new ArgumentNullException(nameof(dnsEndPoint));
}
}

Check warning on line 41 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L41

Added line #L41 was not covered by tests

private BindingEndPoint(string host, int port, IPAddress ipAddress)
Expand Down Expand Up @@ -92,13 +88,8 @@ public static bool TryParse(string endpointStr, out BindingEndPoint endPoint)
return true;

Check warning on line 88 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L86-L88

Added lines #L86 - L88 were not covered by tests
}

private static IPAddress TryGetIPAddressFromHostArgument(string host)
private static IPAddress TryGetIPAddressFromHost(string host)
{
if (host is null)
{
throw new ArgumentNullException(nameof(host));
}

return IPAddress.TryParse(host, out IPAddress ip)
? ip
: null;

Check warning on line 95 in src/SslCertBinding.Net/BindingEndPoint.cs

View check run for this annotation

Codecov / codecov/patch

src/SslCertBinding.Net/BindingEndPoint.cs#L94-L95

Added lines #L94 - L95 were not covered by tests
Expand Down
9 changes: 5 additions & 4 deletions src/SslCertBinding.Net/BindingEndPointExtentions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Net;
using System;
using System.Net;

namespace SslCertBinding.Net
{
public static class BindingEndPointExtentions
{
public static BindingEndPoint ToBindingEndPoint(this IPEndPoint ipEndPoint) => ipEndPoint.ToBindingEndPoint();
public static BindingEndPoint ToBindingEndPoint(this IPEndPoint ipEndPoint) => new BindingEndPoint(ipEndPoint);
public static BindingEndPoint ToBindingEndPoint(this DnsEndPoint dnsEndPoint) => dnsEndPoint as BindingEndPoint ?? new BindingEndPoint(dnsEndPoint);
public static DnsEndPoint ToDnsEndPoint(this IPEndPoint ipEndPoint) => ipEndPoint.ToBindingEndPoint();
public static IPEndPoint ToIPEndPoint(this DnsEndPoint dnsEndPoint) => dnsEndPoint.ToBindingEndPoint().ToIPEndPoint();
public static DnsEndPoint ToDnsEndPoint(this IPEndPoint ipEndPoint) => ipEndPoint.ThrowIfNull(nameof(ipEndPoint)).ToBindingEndPoint();
public static IPEndPoint ToIPEndPoint(this DnsEndPoint dnsEndPoint) => dnsEndPoint.ThrowIfNull(nameof(dnsEndPoint)).ToBindingEndPoint().ToIPEndPoint();
}
}
4 changes: 4 additions & 0 deletions src/SslCertBinding.Net/CertificateBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public CertificateBinding(string certificateThumbprint, string certificateStoreN
{
throw new ArgumentException($"Value cannot be null or empty.", nameof(certificateThumbprint));
}
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}

Thumbprint = certificateThumbprint;
StoreName = certificateStoreName ?? "MY"; // StoreName of null is assumed to be My / Personal. See https://msdn.microsoft.com/en-us/library/windows/desktop/aa364647(v=vs.85).aspx
Expand Down
4 changes: 2 additions & 2 deletions src/SslCertBinding.Net/CertificateBindingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Bind(CertificateBinding binding)
HttpApi.CallHttpApi(
delegate
{
GCHandle sockAddrHandle = SockaddrInterop.CreateSockaddrStructure(((BindingEndPoint)binding.EndPoint).ToIPEndPoint());
GCHandle sockAddrHandle = SockaddrInterop.CreateSockaddrStructure(binding.EndPoint.ToIPEndPoint());
IntPtr pIpPort = sockAddrHandle.AddrOfPinnedObject();
var httpServiceConfigSslKey = new HttpApi.HTTP_SERVICE_CONFIG_SSL_KEY(pIpPort);

Expand Down Expand Up @@ -337,7 +337,7 @@ private static CertificateBinding CreateCertificateBindingInfo(HttpApi.HTTP_SERV
UseDsMappers = HasFlag(configInfo.ParamDesc.DefaultFlags, HttpApi.HTTP_SERVICE_CONFIG_SSL_FLAG.USE_DS_MAPPER),
DoNotPassRequestsToRawFilters = HasFlag(configInfo.ParamDesc.DefaultFlags, HttpApi.HTTP_SERVICE_CONFIG_SSL_FLAG.NO_RAW_FILTER),
};
var result = new CertificateBinding(GetThumbrint(hash), storeName, ipPort.ToDnsEndPoint(), appId, options);
var result = new CertificateBinding(GetThumbrint(hash), storeName, ipPort.ToBindingEndPoint(), appId, options);
return result;
}

Expand Down

0 comments on commit 6eeb048

Please sign in to comment.