Add unit tests for parser
MODIFY
WalletWasabi.Tests/ParserTests.cs
WalletWasabi.Tests/ParserTests.cs
@@ -10,10 +10,10 @@ namespace WalletWasabi.Tests
{
[Theory]
[InlineData("localhost")]
- //[InlineData("127.0.0.1")]
- //[InlineData("192.168.56.1")]
- //[InlineData("foo.com")]
- //[InlineData("foo.onion")]
+ [InlineData("127.0.0.1")]
+ [InlineData("192.168.56.1")]
+ [InlineData("foo.com")]
+ [InlineData("foo.onion")]
public void EndPointParserTests(string host)
{
var inputs = new[]
@@ -32,14 +32,26 @@ namespace WalletWasabi.Tests
$"{host} :5000",
$" {host}:5000",
$"{host}: 5000",
- $" {host} : 5000 "
+ $" {host} : 5000 ",
+ $"{host}/",
+ $"{host}/ ",
+ $" {host}/ ",
+ $"{host}/:5000",
+ $"{host}/:5000/",
+ $"{host}/:5000/ ",
+ $"{host}/: 5000/",
+ $"{host}/ :5000/ ",
+ $"{host}/ : 5000/",
+ $"{host}/ : 5000/ ",
+ $" {host}/ : 5000/ "
};
foreach (var inputString in inputs)
{
var success = EndPointParser.TryParse(inputString, 5000, out EndPoint ep);
Assert.True(success);
- Assert.Equal(5000, ep.GetPortOrDefault());
+ var actualPort = ep.GetPortOrDefault();
+ Assert.Equal(5000, actualPort);
var actualHost = ep.GetHostOrDefault();
var expectedHost = host;
if (host == "localhost")
@@ -47,6 +59,7 @@ namespace WalletWasabi.Tests
expectedHost = "127.0.0.1";
}
Assert.Equal(expectedHost, actualHost);
+ Assert.Equal($"{actualHost}:{actualPort}", ep.ToString(5000));
}
}
}

MODIFY
WalletWasabi/Helpers/EndPointParser.cs
WalletWasabi/Helpers/EndPointParser.cs
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
using WalletWasabi.Helpers;
@@ -41,32 +42,54 @@ namespace System.Net
{
endPoint = null;
- endPointString = Guard.Correct(endPointString);
- endPointString = endPointString.TrimEnd('/');
- endPointString = endPointString.TrimEnd(':');
+ if (string.IsNullOrWhiteSpace(endPointString))
+ {
+ return false;
+ }
- var lastIndex = endPointString.LastIndexOf(':');
+ endPointString = Guard.Correct(endPointString);
+ endPointString = endPointString.TrimEnd(':', '/');
+ var parts = endPointString.Split(':', StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim().TrimEnd('/').TrimEnd()).ToArray();
- string portString = "";
- if (lastIndex != -1)
+ int port;
+ if (parts.Length == 0)
{
- portString = endPointString.Substring(endPointString.LastIndexOf(':') + 1);
+ return false;
}
-
- if (portString is null || !int.TryParse(portString, out int port))
+ else if (parts.Length == 1)
{
if (defaultPort == -1)
{
return false;
}
-
+ else
+ {
port = defaultPort;
}
+ }
+ else if (parts.Length == 2)
+ {
+ var portString = parts[1];
+ if (int.TryParse(portString, out int p))
+ {
+ port = p;
+ }
+ else
+ {
+ if (defaultPort == -1)
+ {
+ return false;
+ }
- var host = endPointString.TrimEnd(portString, StringComparison.OrdinalIgnoreCase);
-
- host = host.TrimEnd(':');
+ port = defaultPort;
+ }
+ }
+ else
+ {
+ return false;
+ }
+ string host = parts[0];
if (host == "localhost")
{
endPoint = new IPEndPoint(IPAddress.Loopback, port);

Add unit tests for parser