Prechádzať zdrojové kódy

refactor: Update tests

Bruce Wayne 5 mesiacov pred
rodič
commit
6fbd4dcad7

+ 7 - 10
UnitTest/StunClien5389UDPTest.cs

@@ -1,4 +1,3 @@
-using Dns.Net.Abstractions;
 using Dns.Net.Clients;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -15,7 +14,7 @@ namespace UnitTest;
 [TestClass]
 public class StunClien5389UDPTest
 {
-	private readonly IDnsClient _dnsClient = new DefaultDnsClient();
+	private readonly DefaultDnsClient _dnsClient = new();
 
 	private static readonly IPEndPoint Any = new(IPAddress.Any, 0);
 	private static readonly IPEndPoint LocalAddress1 = IPEndPoint.Parse(@"127.0.0.1:114");
@@ -115,6 +114,8 @@ public class StunClien5389UDPTest
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
 		await TestAsync();
 
+		return;
+
 		async Task TestAsync()
 		{
 			await client.MappingBehaviorTestAsync();
@@ -190,10 +191,7 @@ public class StunClien5389UDPTest
 			LocalEndPoint = LocalAddress1,
 			OtherEndPoint = ChangedAddress1
 		};
-		StunResult5389 r2 = new()
-		{
-			BindingTestResult = BindingTestResult.Fail,
-		};
+		StunResult5389 r2 = new() { BindingTestResult = BindingTestResult.Fail };
 
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
@@ -309,10 +307,7 @@ public class StunClien5389UDPTest
 			LocalEndPoint = LocalAddress1,
 			OtherEndPoint = ChangedAddress1
 		};
-		StunResult5389 r3 = new()
-		{
-			BindingTestResult = BindingTestResult.Fail
-		};
+		StunResult5389 r3 = new() { BindingTestResult = BindingTestResult.Fail };
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress1, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
@@ -382,6 +377,8 @@ public class StunClien5389UDPTest
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
 		await TestAsync();
 
+		return;
+
 		async Task TestAsync()
 		{
 			await client.FilteringBehaviorTestAsync();

+ 21 - 124
UnitTest/StunClient3489Test.cs

@@ -1,11 +1,9 @@
-using Dns.Net.Abstractions;
 using Dns.Net.Clients;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
 using STUN.Client;
 using STUN.Enums;
 using STUN.Messages;
-using STUN.Utils;
 using System.Net;
 using System.Net.Sockets;
 using static STUN.Utils.AttributeExtensions;
@@ -15,7 +13,7 @@ namespace UnitTest;
 [TestClass]
 public class StunClient3489Test
 {
-	private readonly IDnsClient _dnsClient = new DefaultDnsClient();
+	private readonly DefaultDnsClient _dnsClient = new();
 
 	private const string Server = @"stun.hot-chilli.net";
 	private const ushort Port = 3478;
@@ -54,48 +52,24 @@ public class StunClient3489Test
 		mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(unknownResponse);
 		await TestAsync();
 
-		StunResponse r1 = new(new StunMessage5389
-		{
-			Attributes = new[]
-			{
-				BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-			}
-		}, ServerAddress, LocalAddress1);
+		StunResponse r1 = new(new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] }, ServerAddress, LocalAddress1);
 		mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r1);
 		await TestAsync();
 
-		StunResponse r2 = new(new StunMessage5389
-		{
-			Attributes = new[]
-			{
-				BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-			}
-		}, ServerAddress, LocalAddress1);
+		StunResponse r2 = new(new StunMessage5389 { Attributes = [BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] }, ServerAddress, LocalAddress1);
 		mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r2);
 		await TestAsync();
 
-		StunResponse r3 = new(new StunMessage5389
-		{
-			Attributes = new[]
-			{
-				BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-				BuildChangeAddress(IpFamily.IPv4, ServerAddress.Address, (ushort)ChangedAddress1.Port)
-			}
-		}, ServerAddress, LocalAddress1);
+		StunResponse r3 = new(new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ServerAddress.Address, (ushort)ChangedAddress1.Port)] }, ServerAddress, LocalAddress1);
 		mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r3);
 		await TestAsync();
 
-		StunResponse r4 = new(new StunMessage5389
-		{
-			Attributes = new[]
-			{
-				BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-				BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ServerAddress.Port)
-			}
-		}, ServerAddress, LocalAddress1);
+		StunResponse r4 = new(new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ServerAddress.Port)] }, ServerAddress, LocalAddress1);
 		mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r4);
 		await TestAsync();
 
+		return;
+
 		async Task TestAsync()
 		{
 			await client.QueryAsync();
@@ -110,25 +84,12 @@ public class StunClient3489Test
 		StunClient3489 client = mock.Object;
 
 		StunResponse openInternetTest1Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			MappedAddress1
 		);
 		StunResponse test2Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] },
 			ChangedAddress1,
 			MappedAddress1
 		);
@@ -154,58 +115,27 @@ public class StunClient3489Test
 		StunClient3489 client = mock.Object;
 
 		StunResponse test1Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);
 		StunResponse fullConeResponse = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] },
 			ChangedAddress1,
 			LocalAddress1
 		);
 		StunResponse unsupportedResponse1 = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);
 		StunResponse unsupportedResponse2 = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] },
 			new IPEndPoint(ServerAddress.Address, ChangedAddress1.Port),
 			LocalAddress1
 		);
 		StunResponse unsupportedResponse3 = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)] },
 			new IPEndPoint(ChangedAddress1.Address, ServerAddress.Port),
 			LocalAddress1
 		);
@@ -227,6 +157,8 @@ public class StunClient3489Test
 		mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(unsupportedResponse3);
 		await TestUnsupportedServerAsync();
 
+		return;
+
 		async Task TestUnsupportedServerAsync()
 		{
 			await client.QueryAsync();
@@ -241,26 +173,12 @@ public class StunClient3489Test
 		StunClient3489 client = mock.Object;
 
 		StunResponse test1Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);
 		StunResponse test12Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress2.Address, (ushort)MappedAddress2.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress2.Address, (ushort)MappedAddress2.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);
@@ -286,38 +204,17 @@ public class StunClient3489Test
 		StunClient3489 client = mock.Object;
 
 		StunResponse test1Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);
 		StunResponse test3Response = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ChangedAddress2,
 			LocalAddress1
 		);
 		StunResponse test3ErrorResponse = new(
-			new StunMessage5389
-			{
-				Attributes = new[]
-				{
-					BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
-					BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
-				}
-			},
+			new StunMessage5389 { Attributes = [BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port), BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)] },
 			ServerAddress,
 			LocalAddress1
 		);

+ 3 - 10
UnitTest/StunClient5389TCPTest.cs

@@ -1,4 +1,3 @@
-using Dns.Net.Abstractions;
 using Dns.Net.Clients;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
@@ -15,7 +14,7 @@ namespace UnitTest;
 [TestClass]
 public class StunClient5389TCPTest
 {
-	private readonly IDnsClient _dnsClient = new DefaultDnsClient();
+	private readonly DefaultDnsClient _dnsClient = new();
 
 	private static readonly IPEndPoint Any = new(IPAddress.Any, 0);
 	private static readonly IPEndPoint LocalAddress1 = IPEndPoint.Parse(@"127.0.0.1:114");
@@ -279,10 +278,7 @@ public class StunClient5389TCPTest
 			LocalEndPoint = LocalAddress1,
 			OtherEndPoint = ChangedAddress1
 		};
-		StunResult5389 r2 = new()
-		{
-			BindingTestResult = BindingTestResult.Fail,
-		};
+		StunResult5389 r2 = new() { BindingTestResult = BindingTestResult.Fail };
 
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
@@ -400,10 +396,7 @@ public class StunClient5389TCPTest
 			LocalEndPoint = LocalAddress1,
 			OtherEndPoint = ChangedAddress1
 		};
-		StunResult5389 r3 = new()
-		{
-			BindingTestResult = BindingTestResult.Fail
-		};
+		StunResult5389 r3 = new() { BindingTestResult = BindingTestResult.Fail };
 
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
 		mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);

+ 17 - 23
UnitTest/XorMappedTest.cs

@@ -8,30 +8,24 @@ namespace UnitTest;
 [TestClass]
 public class XorMappedTest
 {
-	private static ReadOnlySpan<byte> MagicCookieAndTransactionId => new byte[]
-	{
+	private static ReadOnlySpan<byte> MagicCookieAndTransactionId =>
+	[
 		0x21, 0x12, 0xa4, 0x42,
 		0xb7, 0xe7, 0xa7, 0x01,
 		0xbc, 0x34, 0xd6, 0x86,
 		0xfa, 0x87, 0xdf, 0xae
-	};
+	];
 
-	private static readonly byte[] XorPort = { 0xa1, 0x47 };
-	private static readonly byte[] XorIPv4 = { 0xe1, 0x12, 0xa6, 0x43 };
-	private static readonly byte[] XorIPv6 =
-	{
-		0x01, 0x13, 0xa9, 0xfa,
-		0xa5, 0xd3, 0xf1, 0x79,
-		0xbc, 0x25, 0xf4, 0xb5,
-		0xbe, 0xd2, 0xb9, 0xd9
-	};
+	private static readonly byte[] XorPort = [0xa1, 0x47];
+	private static readonly byte[] XorIPv4 = [0xe1, 0x12, 0xa6, 0x43];
+	private static readonly byte[] XorIPv6 = [0x01, 0x13, 0xa9, 0xfa, 0xa5, 0xd3, 0xf1, 0x79, 0xbc, 0x25, 0xf4, 0xb5, 0xbe, 0xd2, 0xb9, 0xd9];
 
 	private const ushort Port = 32853;
-	private readonly IPAddress IPv4 = IPAddress.Parse(@"192.0.2.1");
-	private readonly IPAddress IPv6 = IPAddress.Parse(@"2001:db8:1234:5678:11:2233:4455:6677");
+	private readonly IPAddress _ipv4 = IPAddress.Parse(@"192.0.2.1");
+	private readonly IPAddress _ipv6 = IPAddress.Parse(@"2001:db8:1234:5678:11:2233:4455:6677");
 
-	private readonly byte[] _ipv4Response = new byte[] { 0x00, (byte)IpFamily.IPv4 }.Concat(XorPort).Concat(XorIPv4).ToArray();
-	private readonly byte[] _ipv6Response = new byte[] { 0x00, (byte)IpFamily.IPv6 }.Concat(XorPort).Concat(XorIPv6).ToArray();
+	private readonly byte[] _ipv4Response = ((byte[]) [0x00, (byte)IpFamily.IPv4]).Concat(XorPort).Concat(XorIPv4).ToArray();
+	private readonly byte[] _ipv6Response = ((byte[]) [0x00, (byte)IpFamily.IPv6]).Concat(XorPort).Concat(XorIPv6).ToArray();
 
 	/// <summary>
 	/// https://datatracker.ietf.org/doc/html/rfc5769.html
@@ -43,7 +37,7 @@ public class XorMappedTest
 		{
 			Port = Port,
 			Family = IpFamily.IPv4,
-			Address = IPv4
+			Address = _ipv4
 		};
 		Span<byte> temp = stackalloc byte[ushort.MaxValue];
 
@@ -53,15 +47,15 @@ public class XorMappedTest
 
 		t = new XorMappedAddressStunAttributeValue(MagicCookieAndTransactionId);
 		Assert.IsTrue(t.TryParse(_ipv4Response));
-		Assert.AreEqual(t.Port, Port);
-		Assert.AreEqual(t.Family, IpFamily.IPv4);
-		Assert.AreEqual(t.Address, IPv4);
+		Assert.AreEqual(Port, t.Port);
+		Assert.AreEqual(IpFamily.IPv4, t.Family);
+		Assert.AreEqual(_ipv4, t.Address);
 
 		t = new XorMappedAddressStunAttributeValue(MagicCookieAndTransactionId);
 		Assert.IsTrue(t.TryParse(_ipv6Response));
-		Assert.AreEqual(t.Port, Port);
-		Assert.AreEqual(t.Family, IpFamily.IPv6);
-		Assert.AreEqual(t.Address, IPv6);
+		Assert.AreEqual(Port, t.Port);
+		Assert.AreEqual(IpFamily.IPv6, t.Family);
+		Assert.AreEqual(_ipv6, t.Address);
 
 		int length6 = t.WriteTo(temp);
 		Assert.AreNotEqual(0, length6);