Skip to content

Commit

Permalink
test: Add test to verify key created with openssl 3.1 is working. Ver…
Browse files Browse the repository at this point in the history
…ify key types in both tests
  • Loading branch information
zuntio committed Aug 21, 2023
1 parent 60eb15e commit c0a568e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
11 changes: 11 additions & 0 deletions tests/Testcontainers.Tests/Fixtures/Containers/Unix/DockerMTls.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace DotNet.Testcontainers.Tests.Fixtures
{
using System.Collections.Generic;
using System.IO;
using DotNet.Testcontainers.Builders;
using Org.BouncyCastle.OpenSsl;

public abstract class DockerMTls : ProtectDockerDaemonSocket
{
Expand All @@ -10,6 +12,15 @@ public DockerMTls(string dockerImageVersion)
{
}

public object ClientCertificateKey()
{
var path = Path.Combine(_hostCertsDirectoryPath, "client", "key.pem");
using (var keyFileStream = new StreamReader(path))
{
return new PemReader(keyFileStream).ReadObject();
}
}

public override IList<string> CustomProperties
{
get
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DotNet.Testcontainers.Tests.Fixtures
{
using JetBrains.Annotations;

[UsedImplicitly]
public sealed class OpenSsl3_1Fixture : DockerMTls
{
public const string DockerVersion = "24.0.5";
public OpenSsl3_1Fixture() : base(DockerVersion)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public abstract class ProtectDockerDaemonSocket : IAsyncLifetime

private const ushort TlsPort = 2376;

private readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName);

private readonly string _containerCertsDirectoryPath = Path.Combine("/", CertsDirectoryName);

private readonly IContainer _container;

protected readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName);

protected ProtectDockerDaemonSocket(ContainerBuilder containerConfiguration, string dockerImageVersion)
{
_container = containerConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Tests.Fixtures;
using Microsoft.Extensions.Logging.Abstractions;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Xunit;

public static class ProtectDockerDaemonSocketTest
Expand All @@ -20,10 +22,12 @@ private static IDockerEndpointAuthenticationConfiguration GetAuthConfig(ProtectD

public sealed class MTlsOpenSsl1_1_1 : IClassFixture<OpenSsl1_1_1Fixture>
{
private readonly OpenSsl1_1_1Fixture _fixture;
private readonly IDockerEndpointAuthenticationConfiguration _authConfig;

public MTlsOpenSsl1_1_1(OpenSsl1_1_1Fixture dockerMTlsFixture)
{
_fixture = dockerMTlsFixture;
_authConfig = GetAuthConfig(dockerMTlsFixture);
}

Expand All @@ -35,9 +39,38 @@ public async Task GetVersionReturnsVersion()
// When
var version = await client.System.GetVersionAsync()
.ConfigureAwait(false);
var key = _fixture.ClientCertificateKey();

// Then
Assert.Equal(OpenSsl1_1_1Fixture.DockerVersion, version.Version);
Assert.IsType<AsymmetricCipherKeyPair>(key);
}
}

public sealed class MTlsOpenSsl3_1 : IClassFixture<OpenSsl3_1Fixture>
{
private readonly OpenSsl3_1Fixture _fixture;
private readonly IDockerEndpointAuthenticationConfiguration _authConfig;

public MTlsOpenSsl3_1(OpenSsl3_1Fixture dockerMTlsFixture)
{
_fixture = dockerMTlsFixture;
_authConfig = GetAuthConfig(dockerMTlsFixture);
}

[Fact]
public async Task GetVersionReturnsVersion()
{
// Given
var client = new TestcontainersClient(Guid.Empty, _authConfig, NullLogger.Instance);
// When
var version = await client.System.GetVersionAsync()
.ConfigureAwait(false);
var key = _fixture.ClientCertificateKey();

// Then
Assert.Equal(OpenSsl3_1Fixture.DockerVersion, version.Version);
Assert.IsType<RsaPrivateCrtKeyParameters>(key);
}
}

Expand Down

0 comments on commit c0a568e

Please sign in to comment.