Skip to content

Commit

Permalink
Add Secure and SecureOk methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
sanych-sun committed Dec 5, 2023
1 parent f5a82c4 commit f813a85
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/RabbitMQ.Next/Transport/Methods/Connection/SecureMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using RabbitMQ.Next.Methods;

namespace RabbitMQ.Next.Transport.Methods.Connection;

public readonly struct SecureMethod : IIncomingMethod
{
public SecureMethod(ReadOnlyMemory<byte> challenge)
{
this.Challenge = challenge;
}

public MethodId MethodId => MethodId.ConnectionSecure;

public ReadOnlyMemory<byte> Challenge { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;

namespace RabbitMQ.Next.Transport.Methods.Connection;

internal class SecureMethodParser : IMethodParser<SecureMethod>
{
public SecureMethod Parse(ReadOnlySpan<byte> payload)
{
payload.Read(out byte[] challenge);

return new SecureMethod(challenge);
}
}
16 changes: 16 additions & 0 deletions src/RabbitMQ.Next/Transport/Methods/Connection/SecureOkMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using RabbitMQ.Next.Methods;

namespace RabbitMQ.Next.Transport.Methods.Connection;

public readonly struct SecureOkMethod : IOutgoingMethod
{
public SecureOkMethod(ReadOnlyMemory<byte> response)
{
this.Response = response;
}

public MethodId MethodId => MethodId.ConnectionSecureOk;

public ReadOnlyMemory<byte> Response { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace RabbitMQ.Next.Transport.Methods.Connection;

internal class SecureOkMethodFormatter : IMethodFormatter<SecureOkMethod>
{
public Span<byte> Write(Span<byte> destination, SecureOkMethod method)
=> destination.Write(method.Response.Span);
}
2 changes: 2 additions & 0 deletions src/RabbitMQ.Next/Transport/Methods/MethodRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void Register<TMethod>(MethodId methodId, IMethodFormatter<TMethod> formatter =
// ConnectionMethods
Register(MethodId.ConnectionStart, parser: new Connection.StartMethodParser());
Register(MethodId.ConnectionStartOk, formatter: new Connection.StartOkMethodFormatter());
Register(MethodId.ConnectionSecure, parser: new Connection.SecureMethodParser());
Register(MethodId.ConnectionSecureOk, formatter: new Connection.SecureOkMethodFormatter());
Register(MethodId.ConnectionTune, parser: new Connection.TuneMethodParser());
Register(MethodId.ConnectionTuneOk, formatter: new Connection.TuneOkMethodFormatter());
Register(MethodId.ConnectionOpen, formatter: new Connection.OpenMethodFormatter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ public void StartOkMethod()
Assert.Equal(locale, startOkMethod.Locale);
Assert.Equal(clientProperties, startOkMethod.ClientProperties);
}

[Fact]
public void SecureMethod()
{
var challenge = "ping"u8.ToArray();

var secureMethod = new SecureMethod(challenge);

Assert.Equal(MethodId.ConnectionSecure, secureMethod.MethodId);
Assert.Equal(challenge, secureMethod.Challenge);
}

[Fact]
public void SecureOkMethod()
{
var response = "pong"u8.ToArray();

var startOkMethod = new SecureOkMethod(response);

Assert.Equal(MethodId.ConnectionSecureOk, startOkMethod.MethodId);
Assert.Equal(response, startOkMethod.Response);
}

[Fact]
public void TuneMethod()
{
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using RabbitMQ.Next.Tests.Mocks;
using RabbitMQ.Next.Transport.Methods.Connection;
Expand Down Expand Up @@ -54,6 +55,14 @@ public void StartOkMethodFormatter()
this.TestFormatter(method);
}

[Fact]
public void SecureMethodParser()
=> this.TestParser(new SecureMethod("ping"u8.ToArray()), new SecureMethodComparer());

[Fact]
public void SecureOkMethodFormatter()
=> this.TestFormatter(new SecureOkMethod("pong"u8.ToArray()));

[Fact]
public void TuneMethodParser()
=> this.TestParser(new TuneMethod(2047, 131072, 60));
Expand Down Expand Up @@ -110,4 +119,13 @@ public int GetHashCode(StartMethod obj)
return HashCode.Combine(obj.VersionMajor, obj.VersionMinor, obj.ServerProperties, obj.Mechanisms, obj.Locales);
}
}

private class SecureMethodComparer : IEqualityComparer<SecureMethod>
{
public bool Equals(SecureMethod x, SecureMethod y)
=> x.Challenge.Span.SequenceEqual(y.Challenge.Span);

public int GetHashCode(SecureMethod obj)
=> obj.Challenge.GetHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void TestParser<TMethod>(TMethod method, IEqualityComparer<TMethod> co
var payload = Helpers.GetFileContent(payloadResName);

var data = parser.Parse(payload);

Assert.Equal(method, data, comparer);
}
}

0 comments on commit f813a85

Please sign in to comment.