Files
v2rayN/v2rayN/ServiceLib.Tests/Fmt/AnytlsFmtTests.cs
T
Wei-Lun Li ff9fd7b988 fix(fmt): default the AnyTLS and Naive port to 443 when the link omits it (#10504)
The AnyTLS URI scheme makes the port optional and defaults it to 443,
and two of its three examples leave it out. The naive+https:// and
naive+quic:// link format does the same: the port is omitted when it is
443, and none of its examples carries one.

System.Uri answers -1 for an unregistered scheme with no port, so these
links were imported with Port = -1 and the profile failed IsValid().
Apply the same default Hysteria2Fmt already uses (#10026). An explicit
":0" is still kept as 0.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 19:44:03 +08:00

48 lines
2.0 KiB
C#

namespace ServiceLib.Tests.Fmt;
public class AnytlsFmtTests
{
// "服务器的地址和可选端口。如果省略端口,则默认为 443。" (the port is optional and defaults to 443)
// -- https://github.com/anytls/anytls-go/blob/main/docs/uri_scheme.md
// The first two cases are the examples given there.
[Test]
[Arguments("anytls://letmein@example.com/?sni=real.example.com")]
[Arguments("anytls://letmein@example.com/?sni=127.0.0.1&insecure=1")]
[Arguments("anytls://letmein@example.com")]
[Arguments("anytls://letmein@example.com:/")]
public async Task ResolveConfig_WithoutPort_ShouldDefaultTo443(string shareUri)
{
var resolved = FmtHandler.ResolveConfig(shareUri, out var msg);
await resolved.Should().NotBeNull().Because($"uri: {shareUri}, msg: {msg}");
await resolved!.ConfigType.Should().BeEqualTo(EConfigType.Anytls);
await resolved.Address.Should().BeEqualTo("example.com");
await resolved.Port.Should().BeEqualTo(443);
await resolved.Password.Should().BeEqualTo("letmein");
await resolved.IsValid().Should().BeTrue();
}
[Test]
public async Task ResolveConfig_WithExplicitPort_ShouldKeepIt()
{
// The third example from the URI scheme document.
const string shareUri = "anytls://0fdf77d7-d4ba-455e-9ed9-a98dd6d5489a@[2409:8a71:6a00:1953::615]:8964/?insecure=1";
var resolved = FmtHandler.ResolveConfig(shareUri, out var msg);
await resolved.Should().NotBeNull().Because($"uri: {shareUri}, msg: {msg}");
await resolved!.Address.Should().BeEqualTo("2409:8a71:6a00:1953::615");
await resolved.Port.Should().BeEqualTo(8964);
}
[Test]
public async Task ResolveConfig_WithExplicitZeroPort_ShouldNotApplyTheDefault()
{
var resolved = FmtHandler.ResolveConfig("anytls://letmein@example.com:0/", out _);
await resolved.Should().NotBeNull();
await resolved!.Port.Should().BeEqualTo(0);
await resolved.IsValid().Should().BeFalse();
}
}