Skip to content

Commit

Permalink
Merge pull request #26 from viordash/settings
Browse files Browse the repository at this point in the history
to more strict mdns params
  • Loading branch information
viordash authored Oct 1, 2023
2 parents 75438fb + 2a78619 commit 2fafb57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public void UseAddressDiscoveryService_Valid_Address_Return_True_And_Id() {
public void UseAddressDiscoveryService_When_Invalid_Ports_Throws_ArgumentException() {
string id;
int? mandatoryPort;
Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde:", out id, out mandatoryPort));
Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde: ", out id, out mandatoryPort));
Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde: 1", out id, out mandatoryPort));
Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde: 65535", out id, out mandatoryPort));

Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde:-1", out id, out mandatoryPort));
Assert.Throws<ArgumentException>(() => AddressResolver.UseAddressDiscoveryService("mdns:abcde:65536", out id, out mandatoryPort));

Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns::1", out id, out mandatoryPort));
Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns::", out id, out mandatoryPort));
Assert.That(id, Is.Empty);
Assert.That(mandatoryPort, Is.Null);
}
Expand All @@ -67,9 +68,17 @@ public void UseAddressDiscoveryService_Returns_True_And_Empty_Id_If_Address_Is_N
string id;
int? mandatoryPort;

Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde:", out id, out mandatoryPort));
Assert.That(id, Is.EqualTo("abcde"));
Assert.That(mandatoryPort, Is.Null);

Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:", out id, out mandatoryPort));
Assert.That(id, Is.Empty);
Assert.That(mandatoryPort, Is.Null);

Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns::1", out id, out mandatoryPort));
Assert.That(id, Is.Empty);
Assert.That(mandatoryPort, Is.EqualTo(1));
}

[Test]
Expand All @@ -85,9 +94,6 @@ public void UseAddressDiscoveryService_Valid_Ports_And_Entire_Id_Extracting_Test
Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde:65535", out id, out mandatoryPort));
Assert.That(id, Is.EqualTo("abcde"));
Assert.That(mandatoryPort, Is.EqualTo(65535));
Assert.IsTrue(AddressResolver.UseAddressDiscoveryService("mdns:abcde: 65535", out id, out mandatoryPort));
Assert.That(id, Is.EqualTo("abcde"));
Assert.That(mandatoryPort, Is.EqualTo(65535));
}
}
}
31 changes: 17 additions & 14 deletions ShareClipbrd/ShareClipbrd.Core/Services/AddressResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Globalization;
using System.Net;

namespace ShareClipbrd.Core.Services {
public class AddressResolver {
Expand All @@ -11,17 +12,23 @@ static void MandatoryPort(ref string id, out int? mandatoryPort) {
mandatoryPort = null;
return;
}
if(portStart == id.Length - 1) {
id = id[..portStart];
mandatoryPort = null;
return;
}

if(int.TryParse(id[(portStart + 1)..], out port)) {
try {
_ = new IPEndPoint(IPAddress.Any, port);
id = id[..portStart];
mandatoryPort = port;
return;
} catch(ArgumentOutOfRangeException) {
}
try {
port = int.Parse(id[(portStart + 1)..], NumberStyles.None, CultureInfo.InvariantCulture);
_ = new IPEndPoint(IPAddress.Any, port);
id = id[..portStart];
mandatoryPort = port;
return;
} catch(ArgumentOutOfRangeException) {
throw new ArgumentException($"Port not valid");
} catch(FormatException) {
throw new ArgumentException($"Port not valid");
}
throw new ArgumentException("mdns port for the partner address is not needed");
}

public static bool UseAddressDiscoveryService(string address, out string id, out int? mandatoryPort) {
Expand All @@ -35,10 +42,6 @@ public static bool UseAddressDiscoveryService(string address, out string id, out

MandatoryPort(ref s, out mandatoryPort);
s = s.Trim();
if(string.IsNullOrEmpty(s)) {
id = string.Empty;
mandatoryPort = null;
}
id = s;
return true;
}
Expand Down

0 comments on commit 2fafb57

Please sign in to comment.