140 lines
4.8 KiB
C#
140 lines
4.8 KiB
C#
using System.IO;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using TermRemoteCtl.Agent.Configuration;
|
|
|
|
namespace TermRemoteCtl.Agent.Tests.Configuration;
|
|
|
|
public class AgentOptionsPipelineTests
|
|
{
|
|
[Fact]
|
|
public void BuildListenUri_Uses_Https_Scheme_And_HttpsPort()
|
|
{
|
|
var options = new AgentOptions
|
|
{
|
|
DataRoot = "C:\\ProgramData\\TermRemoteCtl",
|
|
BindAddress = "127.0.0.1",
|
|
HttpsPort = 9443,
|
|
HttpPort = 5067,
|
|
WebSocketFrameFlushMilliseconds = 33,
|
|
RingBufferLineLimit = 4000
|
|
};
|
|
|
|
var uri = AgentEndpointConfiguration.BuildListenUri(options);
|
|
|
|
Assert.Equal(Uri.UriSchemeHttps, uri.Scheme);
|
|
Assert.Equal("127.0.0.1", uri.Host);
|
|
Assert.Equal(9443, uri.Port);
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildListenUri_Uses_Http_Scheme_When_HttpsPort_Is_Disabled()
|
|
{
|
|
var options = new AgentOptions
|
|
{
|
|
DataRoot = "C:\\ProgramData\\TermRemoteCtl",
|
|
BindAddress = "10.0.2.2",
|
|
HttpsPort = 0,
|
|
HttpPort = 5067,
|
|
WebSocketFrameFlushMilliseconds = 33,
|
|
RingBufferLineLimit = 4000
|
|
};
|
|
|
|
var uri = AgentEndpointConfiguration.BuildListenUri(options);
|
|
|
|
Assert.Equal(Uri.UriSchemeHttp, uri.Scheme);
|
|
Assert.Equal("10.0.2.2", uri.Host);
|
|
Assert.Equal(5067, uri.Port);
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveOptions_Uses_DataRoot_Fallback_When_Config_Omits_DataRoot()
|
|
{
|
|
var configuration = BuildConfiguration(new Dictionary<string, string?>
|
|
{
|
|
["Agent:BindAddress"] = "127.0.0.1",
|
|
["Agent:HttpsPort"] = "9443",
|
|
["Agent:HttpPort"] = "5067",
|
|
["Agent:WebSocketFrameFlushMilliseconds"] = "33",
|
|
["Agent:RingBufferLineLimit"] = "4000"
|
|
});
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddAgentOptions(configuration);
|
|
|
|
using var provider = services.BuildServiceProvider();
|
|
var options = provider.GetRequiredService<IOptions<AgentOptions>>().Value;
|
|
|
|
var expectedDataRoot = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
|
|
"TermRemoteCtl");
|
|
|
|
Assert.Equal(expectedDataRoot, options.DataRoot);
|
|
Assert.Equal(5067, options.HttpPort);
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveOptions_Allows_Loopback_Scoped_Http_Only_Local_Test_Mode()
|
|
{
|
|
var configuration = BuildConfiguration(new Dictionary<string, string?>
|
|
{
|
|
["Agent:BindAddress"] = "localhost",
|
|
["Agent:HttpsPort"] = "0",
|
|
["Agent:HttpPort"] = "5067",
|
|
["Agent:WebSocketFrameFlushMilliseconds"] = "33",
|
|
["Agent:RingBufferLineLimit"] = "4000"
|
|
});
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddAgentOptions(configuration);
|
|
|
|
using var provider = services.BuildServiceProvider();
|
|
var options = provider.GetRequiredService<IOptions<AgentOptions>>().Value;
|
|
|
|
Assert.Equal(0, options.HttpsPort);
|
|
Assert.Equal(5067, options.HttpPort);
|
|
Assert.Equal("localhost", options.BindAddress);
|
|
Assert.Equal(Uri.UriSchemeHttp, AgentEndpointConfiguration.BuildListenUri(options).Scheme);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("0", "0", "33", "HttpsPort")]
|
|
[InlineData("5067", "5067", "33", "must not be the same")]
|
|
[InlineData("9443", "-1", "33", "HttpPort")]
|
|
[InlineData("9443", "5067", "0", "WebSocketFrameFlushMilliseconds")]
|
|
[InlineData("9443", "5067", "33", "RingBufferLineLimit")]
|
|
public void ResolveOptions_Fails_When_Config_Is_Invalid(
|
|
string httpsPort,
|
|
string httpPort,
|
|
string flushMilliseconds,
|
|
string expectedFailure)
|
|
{
|
|
var configuration = BuildConfiguration(new Dictionary<string, string?>
|
|
{
|
|
["Agent:BindAddress"] = "127.0.0.1",
|
|
["Agent:HttpsPort"] = httpsPort,
|
|
["Agent:HttpPort"] = httpPort,
|
|
["Agent:WebSocketFrameFlushMilliseconds"] = flushMilliseconds,
|
|
["Agent:RingBufferLineLimit"] = expectedFailure == "RingBufferLineLimit" ? "0" : "4000"
|
|
});
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddAgentOptions(configuration);
|
|
|
|
using var provider = services.BuildServiceProvider();
|
|
|
|
var exception = Assert.Throws<OptionsValidationException>(
|
|
() => provider.GetRequiredService<IOptions<AgentOptions>>().Value);
|
|
|
|
Assert.Contains(exception.Failures, failure => failure.Contains(expectedFailure));
|
|
}
|
|
|
|
private static IConfiguration BuildConfiguration(IReadOnlyDictionary<string, string?> values)
|
|
{
|
|
return new ConfigurationBuilder()
|
|
.AddInMemoryCollection(values)
|
|
.Build();
|
|
}
|
|
}
|