Skip to content

Commit 1aa4ff1

Browse files
committed
test(transport): add tests
1 parent 5ce106b commit 1aa4ff1

4 files changed

Lines changed: 173 additions & 16 deletions

File tree

src/Servus.Akka.Tests/Transport/Tcp/Listener/TcpServerStateMachineSpec.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ private static (TcpServerStateMachine Sm, MockTransportOperations Ops) CreateSta
2323
return (sm, ops);
2424
}
2525

26+
private static (TcpServerStateMachine Sm, MockTransportOperations Ops) CreateStateMachineWithTls(
27+
bool allowDelayedNegotiation)
28+
{
29+
var ops = new MockTransportOperations();
30+
var state = new ClientState(Stream.Null);
31+
var sm = new TcpServerStateMachine(ops, ActorRefs.Nobody, state, TestConnectionInfo,
32+
sslStream: null, allowDelayedNegotiation: allowDelayedNegotiation);
33+
return (sm, ops);
34+
}
35+
2636
private static TransportBuffer CreateTestBuffer(params byte[] data)
2737
{
2838
var buf = TransportBuffer.Rent(data.Length);
@@ -289,4 +299,40 @@ public void Dispatch_InboundComplete_error_should_push_error_disconnected()
289299
var disconnected = Assert.IsType<TransportDisconnected>(ops.PushedInbound[0]);
290300
Assert.Equal(DisconnectReason.Error, disconnected.Reason);
291301
}
302+
303+
[Fact(Timeout = 5000)]
304+
public void Start_should_not_emit_TransportTlsState_without_tls()
305+
{
306+
var (sm, ops) = CreateStateMachine();
307+
308+
sm.Start();
309+
310+
Assert.Single(ops.PushedInbound);
311+
Assert.IsType<TransportConnected>(ops.PushedInbound[0]);
312+
}
313+
314+
[Fact(Timeout = 5000)]
315+
public void Start_should_emit_TransportTlsState_when_allow_delayed_is_true()
316+
{
317+
var (sm, ops) = CreateStateMachineWithTls(allowDelayedNegotiation: true);
318+
319+
sm.Start();
320+
321+
Assert.Equal(2, ops.PushedInbound.Count);
322+
Assert.IsType<TransportConnected>(ops.PushedInbound[0]);
323+
var tlsState = Assert.IsType<TransportTlsState>(ops.PushedInbound[1]);
324+
Assert.True(tlsState.AllowDelayedNegotiation);
325+
Assert.Null(tlsState.SslStream);
326+
}
327+
328+
[Fact(Timeout = 5000)]
329+
public void Start_should_not_emit_TransportTlsState_when_no_ssl_and_no_delay()
330+
{
331+
var (sm, ops) = CreateStateMachineWithTls(allowDelayedNegotiation: false);
332+
333+
sm.Start();
334+
335+
Assert.Single(ops.PushedInbound);
336+
Assert.IsType<TransportConnected>(ops.PushedInbound[0]);
337+
}
292338
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Net.Security;
2+
using Servus.Akka.Transport;
3+
4+
namespace Servus.Akka.Tests.Transport;
5+
6+
public sealed class TcpListenerOptionsSpec
7+
{
8+
[Fact(Timeout = 5000)]
9+
public void TcpListenerOptions_should_default_client_certificate_mode_to_no_certificate()
10+
{
11+
var options = new TcpListenerOptions { Host = "localhost", Port = 443 };
12+
13+
Assert.Equal(ClientCertificateMode.NoCertificate, options.ClientCertificateMode);
14+
}
15+
16+
[Fact(Timeout = 5000)]
17+
public void TcpListenerOptions_should_default_handshake_callback_to_null()
18+
{
19+
var options = new TcpListenerOptions { Host = "localhost", Port = 443 };
20+
21+
Assert.Null(options.HandshakeCallback);
22+
}
23+
24+
[Fact(Timeout = 5000)]
25+
public void TcpListenerOptions_should_default_server_certificate_selector_to_null()
26+
{
27+
var options = new TcpListenerOptions { Host = "localhost", Port = 443 };
28+
29+
Assert.Null(options.ServerCertificateSelector);
30+
}
31+
32+
[Fact(Timeout = 5000)]
33+
public void TcpListenerOptions_should_allow_setting_client_certificate_mode()
34+
{
35+
var options = new TcpListenerOptions
36+
{
37+
Host = "localhost",
38+
Port = 443,
39+
ClientCertificateMode = ClientCertificateMode.RequireCertificate
40+
};
41+
42+
Assert.Equal(ClientCertificateMode.RequireCertificate, options.ClientCertificateMode);
43+
}
44+
}

src/Servus.Akka.Tests/Transport/TransportMessagesSpec.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,82 @@ public void ConnectionInfo_should_allow_null_security()
127127

128128
Assert.Null(info.Security);
129129
}
130+
131+
[Fact(Timeout = 5000)]
132+
public void SecurityInfo_should_default_negotiated_cipher_suite_to_null()
133+
{
134+
var info = new SecurityInfo(SslProtocols.Tls13, SslApplicationProtocol.Http2);
135+
136+
Assert.Null(info.NegotiatedCipherSuite);
137+
}
138+
139+
[Fact(Timeout = 5000)]
140+
public void SecurityInfo_should_default_hostname_to_null()
141+
{
142+
var info = new SecurityInfo(SslProtocols.Tls13, SslApplicationProtocol.Http2);
143+
144+
Assert.Null(info.HostName);
145+
}
146+
147+
[Fact(Timeout = 5000)]
148+
public void SecurityInfo_should_store_negotiated_cipher_suite()
149+
{
150+
var info = new SecurityInfo(
151+
SslProtocols.Tls13,
152+
SslApplicationProtocol.Http2,
153+
TlsCipherSuite.TLS_AES_256_GCM_SHA384);
154+
155+
Assert.Equal(TlsCipherSuite.TLS_AES_256_GCM_SHA384, info.NegotiatedCipherSuite);
156+
}
157+
158+
[Fact(Timeout = 5000)]
159+
public void SecurityInfo_should_store_hostname()
160+
{
161+
var info = new SecurityInfo(
162+
SslProtocols.Tls13,
163+
SslApplicationProtocol.Http2,
164+
HostName: "example.com");
165+
166+
Assert.Equal("example.com", info.HostName);
167+
}
168+
169+
[Fact(Timeout = 5000)]
170+
public void SecurityInfo_should_store_all_fields()
171+
{
172+
var info = new SecurityInfo(
173+
SslProtocols.Tls13,
174+
SslApplicationProtocol.Http2,
175+
TlsCipherSuite.TLS_AES_128_GCM_SHA256,
176+
"host.example.com");
177+
178+
Assert.Equal(SslProtocols.Tls13, info.Protocol);
179+
Assert.Equal(SslApplicationProtocol.Http2, info.ApplicationProtocol);
180+
Assert.Equal(TlsCipherSuite.TLS_AES_128_GCM_SHA256, info.NegotiatedCipherSuite);
181+
Assert.Equal("host.example.com", info.HostName);
182+
}
183+
184+
[Fact(Timeout = 5000)]
185+
public void TransportTlsState_should_implement_ITransportInbound()
186+
{
187+
ITransportInbound msg = new TransportTlsState(SslStream: null, AllowDelayedNegotiation: false);
188+
189+
Assert.IsType<TransportTlsState>(msg);
190+
}
191+
192+
[Fact(Timeout = 5000)]
193+
public void TransportTlsState_should_carry_allow_delayed_flag()
194+
{
195+
var msg = new TransportTlsState(SslStream: null, AllowDelayedNegotiation: true);
196+
197+
Assert.True(msg.AllowDelayedNegotiation);
198+
}
199+
200+
[Fact(Timeout = 5000)]
201+
public void TransportTlsState_should_allow_null_ssl_stream()
202+
{
203+
var msg = new TransportTlsState(SslStream: null, AllowDelayedNegotiation: false);
204+
205+
Assert.Null(msg.SslStream);
206+
Assert.False(msg.AllowDelayedNegotiation);
207+
}
130208
}

src/Servus.Akka/Transport/Tcp/Listener/TcpListenerStage.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Logic(TcpListenerStage stage) : base(stage.Shape)
4848
{
4949
_stage = stage;
5050

51-
SetHandler(stage._out, onPull: () => TryPush());
51+
SetHandler(stage._out, onPull: TryPush);
5252
}
5353

5454
public override void PreStart()
@@ -167,17 +167,6 @@ private async Task InitializeConnectionAsync(TcpClient client)
167167
var localEndPoint = client.Client.LocalEndPoint!;
168168
var remoteEndPoint = client.Client.RemoteEndPoint!;
169169

170-
SecurityInfo? security = null;
171-
var protocol = TransportProtocol.Tcp;
172-
173-
if (stream is SslStream sslStream)
174-
{
175-
security = new SecurityInfo(
176-
sslStream.SslProtocol,
177-
sslStream.NegotiatedApplicationProtocol);
178-
protocol = TransportProtocol.Tls;
179-
}
180-
181170
var connectionInfo = new ConnectionInfo(
182171
localEndPoint,
183172
remoteEndPoint,
@@ -220,8 +209,8 @@ private async Task<TlsConnectionResult> GetTlsStreamAsync(TcpClient client)
220209
return await AuthenticateWithOptionsAsync(client, options);
221210
}
222211

223-
private async Task<TlsConnectionResult> AuthenticateWithCallbackAsync(
224-
TcpClient client, TcpListenerOptions options)
212+
private static async Task<TlsConnectionResult> AuthenticateWithCallbackAsync(TcpClient client,
213+
TcpListenerOptions options)
225214
{
226215
var sslStream = new SslStream(
227216
client.GetStream(),
@@ -232,7 +221,7 @@ private async Task<TlsConnectionResult> AuthenticateWithCallbackAsync(
232221
var allowDelayed = false;
233222

234223
await sslStream.AuthenticateAsServerAsync(
235-
async (stream, clientHelloInfo, state, ct) =>
224+
async (_, clientHelloInfo, _, ct) =>
236225
{
237226
hostname = clientHelloInfo.ServerName;
238227
var context = new TlsHandshakeContext(
@@ -323,4 +312,4 @@ private void OnAcceptError(Exception ex)
323312
FailStage(ex);
324313
}
325314
}
326-
}
315+
}

0 commit comments

Comments
 (0)