Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS first test to skip unsupported server #158

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions tests/NATS.Client.Core.Tests/TlsFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

public TlsFirstTest(ITestOutputHelper output) => _output = output;

[Fact]
public async Task Tls_first_connection()
[SkipIfNatsServer(doesNotSupportTlsFirst: true)]
public async Task Implicit_TLS_connection()
{
if (!NatsServer.SupportsTlsFirst())
{
_output.WriteLine($"TLS first is NOT supported by the server");
return;
}

_output.WriteLine($"TLS first is supported by the server");

await using var server = NatsServer.Start(
new NullOutputHelper(),
new NatsServerOptsBuilder()
Expand Down Expand Up @@ -47,4 +39,40 @@
_output.WriteLine($"Auto TLS connection rejected");
}
}

[Fact]
public async Task Implicit_TLS_fails_when_disabled()
{
await using var server = NatsServer.Start(
new NullOutputHelper(),
new NatsServerOptsBuilder()
.UseTransport(TransportType.Tls, tlsFirst: false)
.Build());

var clientOpts = server.ClientOpts(NatsOpts.Default);

Assert.True(clientOpts.TlsOpts.Mode == TlsMode.Auto);

// TLS first connection should fail
{
await using var nats = new NatsConnection(clientOpts with { TlsOpts = clientOpts.TlsOpts with { Mode = TlsMode.Implicit } });

var exception = await Assert.ThrowsAsync<NatsException>(async () => await nats.ConnectAsync());

Assert.Matches(@"can not start to connect nats server", exception.Message);

_output.WriteLine($"Implicit TLS connection rejected");

}

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 66 in tests/NATS.Client.Core.Tests/TlsFirstTest.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)


// Normal TLS connection should work
{
await using var nats = new NatsConnection(clientOpts);
await nats.ConnectAsync();
var rtt = await nats.PingAsync();
Assert.True(rtt > TimeSpan.Zero);
Assert.True(nats.ServerInfo!.TlsRequired);
_output.WriteLine($"Explicit TLS connection (RTT: {rtt})");
}
}
}
15 changes: 15 additions & 0 deletions tests/NATS.Client.TestUtilities/NatsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,18 @@ public void WriteLine(string format, params object[] args)
{
}
}

public sealed class SkipIfNatsServer : FactAttribute
{
private static readonly bool SupportsTlsFirst;

static SkipIfNatsServer() => SupportsTlsFirst = NatsServer.SupportsTlsFirst();

public SkipIfNatsServer(bool doesNotSupportTlsFirst = false)
{
if (doesNotSupportTlsFirst && !SupportsTlsFirst)
{
Skip = "NATS server doesn't support TLS first";
}
}
}