Skip to content

Commit

Permalink
added unit tests for ProxyProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 10, 2024
1 parent d6c75cd commit d09bbb3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 9 deletions.
11 changes: 7 additions & 4 deletions test/SuperSocket.Tests/FixedHeaderProtocolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ protected override IServer CreateServer(IHostConfigurator hostConfigurator)
})
.ConfigureAppConfiguration((HostBuilder, configBuilder) =>
{
configBuilder.AddInMemoryCollection(new Dictionary<string, string>
{
{ "serverOptions:values:enableSendingPipe", "true" }
});
configBuilder.AddInMemoryCollection(LoadMemoryConfig(new Dictionary<string, string>()));
}).BuildAsServer() as IServer;
}

protected virtual Dictionary<string, string> LoadMemoryConfig(Dictionary<string, string> configSettings)
{
configSettings["serverOptions:values:enableSendingPipe"] = "true";
return configSettings;
}
}
}
16 changes: 11 additions & 5 deletions test/SuperSocket.Tests/ProtocolTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.Server;
using SuperSocket.Server.Abstractions;
Expand All @@ -24,6 +25,11 @@ protected ProtocolTestBase(ITestOutputHelper outputHelper) : base(outputHelper)

protected abstract IServer CreateServer(IHostConfigurator hostConfigurator);

protected virtual IHostConfigurator CreateHostConfigurator(Type hostConfiguratorType)
{
return CreateObject<IHostConfigurator>(hostConfiguratorType);
}

protected abstract string CreateRequest(string sourceLine);

[Theory]
Expand All @@ -35,7 +41,7 @@ protected ProtocolTestBase(ITestOutputHelper outputHelper) : base(outputHelper)
[InlineData(typeof(KestralConnectionHostConfigurator))]
public virtual async Task TestNormalRequest(Type hostConfiguratorType)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 42 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestNormalRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var hostConfigurator = CreateHostConfigurator(hostConfiguratorType);

using (var server = CreateServer(hostConfigurator))
{
Expand Down Expand Up @@ -69,7 +75,7 @@ public virtual async Task TestNormalRequest(Type hostConfiguratorType)
[InlineData(typeof(KestralConnectionHostConfigurator))]
public virtual async Task TestMiddleBreak(Type hostConfiguratorType)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 76 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestMiddleBreak' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var hostConfigurator = CreateHostConfigurator(hostConfiguratorType);

using (var server = CreateServer(hostConfigurator))
{
Expand Down Expand Up @@ -105,7 +111,7 @@ public virtual async Task TestMiddleBreak(Type hostConfiguratorType)
[InlineData(typeof(KestralConnectionHostConfigurator))]
public virtual async Task TestFragmentRequest(Type hostConfiguratorType)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 112 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestFragmentRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var hostConfigurator = CreateHostConfigurator(hostConfiguratorType);

using (var server = CreateServer(hostConfigurator))
{
Expand Down Expand Up @@ -146,7 +152,7 @@ public virtual async Task TestFragmentRequest(Type hostConfiguratorType)
[InlineData(typeof(KestralConnectionHostConfigurator))]
public virtual async Task TestBatchRequest(Type hostConfiguratorType)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 153 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestBatchRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var hostConfigurator = CreateHostConfigurator(hostConfiguratorType);

using (var server = CreateServer(hostConfigurator))
{
Expand Down Expand Up @@ -195,7 +201,7 @@ public virtual async Task TestBatchRequest(Type hostConfiguratorType)
//[InlineData(typeof(UdpHostConfigurator))]
public virtual async Task TestBreakRequest(Type hostConfiguratorType)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)

Check warning on line 202 in test/SuperSocket.Tests/ProtocolTestBase.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Public method 'TestBreakRequest' on test class 'ProtocolTestBase' should be marked as a Theory. (https://xunit.github.io/xunit.analyzers/rules/xUnit1013)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
var hostConfigurator = CreateHostConfigurator(hostConfiguratorType);

using (var server = CreateServer(hostConfigurator))
{
Expand Down
74 changes: 74 additions & 0 deletions test/SuperSocket.Tests/ProxyProtocolHostConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.Client;
using SuperSocket.Connection;
using SuperSocket.ProtoBase;
using SuperSocket.Server.Abstractions;
using SuperSocket.Server.Abstractions.Host;

namespace SuperSocket.Tests
{
public class ProxyProtocolHostConfigurator : IHostConfigurator
{
private IHostConfigurator _innerHostConfigurator;

private static readonly byte[] _proxyProtocolV2_IPV4_SampleData = new byte[]
{
0x0D, 0x0A, 0x0D, 0x0A,
0x00, 0x0D, 0x0A, 0x51,
0x55, 0x49, 0x54, 0x0A,
0x21, 0x11, 0x00, 0x0c,
0xac, 0x13, 0x00, 0x01,
0xac, 0x13, 0x00, 0x03,
0xa6, 0x52, 0x00, 0x50
};

public ProxyProtocolHostConfigurator(IHostConfigurator hostConfigurator)
{
_innerHostConfigurator = hostConfigurator;
}

public string WebSocketSchema => _innerHostConfigurator.WebSocketSchema;

public bool IsSecure => _innerHostConfigurator.IsSecure;

public ListenOptions Listener => _innerHostConfigurator.Listener;

public void Configure(ISuperSocketHostBuilder hostBuilder)
{
_innerHostConfigurator.Configure(hostBuilder);
}

public IEasyClient<TPackageInfo> ConfigureEasyClient<TPackageInfo>(IPipelineFilter<TPackageInfo> pipelineFilter, ConnectionOptions options) where TPackageInfo : class
{
return _innerHostConfigurator.ConfigureEasyClient<TPackageInfo>(pipelineFilter, options);
}

public Socket CreateClient()
{
return _innerHostConfigurator.CreateClient();
}

public async ValueTask<Stream> GetClientStream(Socket socket)
{
var stream = await _innerHostConfigurator.GetClientStream(socket);

await stream.WriteAsync(_proxyProtocolV2_IPV4_SampleData, 0, _proxyProtocolV2_IPV4_SampleData.Length);
await stream.FlushAsync();

return stream;
}

public TextReader GetStreamReader(Stream stream, Encoding encoding)
{
return _innerHostConfigurator.GetStreamReader(stream, encoding);
}

public ValueTask KeepSequence()
{
return _innerHostConfigurator.KeepSequence();
}
}
}
36 changes: 36 additions & 0 deletions test/SuperSocket.Tests/ProxyProtocolTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.ProtoBase;
using SuperSocket.Server.Host;
using SuperSocket.Server.Abstractions;
using Xunit;
using Xunit.Abstractions;

namespace SuperSocket.Tests
{
[Trait("Category", "ProxyProtocol")]
public class ProxyProtocolTest : FixedHeaderProtocolTest
{
public ProxyProtocolTest(ITestOutputHelper outputHelper)
: base(outputHelper)
{
}

protected override IHostConfigurator CreateHostConfigurator(Type hostConfiguratorType)
{
return new ProxyProtocolHostConfigurator(base.CreateHostConfigurator(hostConfiguratorType));
}

protected override Dictionary<string, string> LoadMemoryConfig(Dictionary<string, string> configSettings)
{
base.LoadMemoryConfig(configSettings);
configSettings["serverOptions:enableProxyProtocol"] = "true";
return configSettings;
}
}
}

0 comments on commit d09bbb3

Please sign in to comment.