Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to more strict mdns params #26

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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