StunClient3489Test.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using Dns.Net.Abstractions;
  2. using Dns.Net.Clients;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using STUN.Client;
  6. using STUN.Enums;
  7. using STUN.Messages;
  8. using STUN.Utils;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using static STUN.Utils.AttributeExtensions;
  12. namespace UnitTest;
  13. [TestClass]
  14. public class StunClient3489Test
  15. {
  16. private readonly IDnsClient _dnsClient = new DefaultDnsClient();
  17. private const string Server = @"stun.syncthing.net";
  18. private const ushort Port = 3478;
  19. private static readonly IPEndPoint Any = new(IPAddress.Any, 0);
  20. private static readonly IPEndPoint IPv6Any = new(IPAddress.IPv6Any, 0);
  21. private static readonly IPEndPoint LocalAddress1 = IPEndPoint.Parse(@"127.0.0.1:114");
  22. private static readonly IPEndPoint MappedAddress1 = IPEndPoint.Parse(@"1.1.1.1:114");
  23. private static readonly IPEndPoint MappedAddress2 = IPEndPoint.Parse(@"1.1.1.1:514");
  24. private static readonly IPEndPoint ServerAddress = IPEndPoint.Parse(@"2.2.2.2:1919");
  25. private static readonly IPEndPoint ChangedAddress1 = IPEndPoint.Parse(@"3.3.3.3:23333");
  26. private static readonly IPEndPoint ChangedAddress2 = IPEndPoint.Parse(@"2.2.2.2:810");
  27. private static readonly StunMessage5389 DefaultStunMessage = new();
  28. [TestMethod]
  29. public async Task UdpBlockedTestAsync()
  30. {
  31. Mock<StunClient3489> mock = new(Any, Any, default!);
  32. StunClient3489 client = mock.Object;
  33. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  34. await client.QueryAsync();
  35. Assert.AreEqual(NatType.UdpBlocked, client.State.NatType);
  36. }
  37. [TestMethod]
  38. public async Task UnsupportedServerTestAsync()
  39. {
  40. Mock<StunClient3489> mock = new(Any, Any, default!);
  41. StunClient3489? client = mock.Object;
  42. mock.Setup(x => x.LocalEndPoint).Returns(LocalAddress1);
  43. StunResponse unknownResponse = new(DefaultStunMessage, Any, LocalAddress1);
  44. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(unknownResponse);
  45. await TestAsync();
  46. StunResponse r1 = new(new StunMessage5389
  47. {
  48. Attributes = new[]
  49. {
  50. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  51. }
  52. }, ServerAddress, LocalAddress1);
  53. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r1);
  54. await TestAsync();
  55. StunResponse r2 = new(new StunMessage5389
  56. {
  57. Attributes = new[]
  58. {
  59. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  60. }
  61. }, ServerAddress, LocalAddress1);
  62. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r2);
  63. await TestAsync();
  64. StunResponse r3 = new(new StunMessage5389
  65. {
  66. Attributes = new[]
  67. {
  68. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  69. BuildChangeAddress(IpFamily.IPv4, ServerAddress.Address, (ushort)ChangedAddress1.Port)
  70. }
  71. }, ServerAddress, LocalAddress1);
  72. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r3);
  73. await TestAsync();
  74. StunResponse r4 = new(new StunMessage5389
  75. {
  76. Attributes = new[]
  77. {
  78. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  79. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ServerAddress.Port)
  80. }
  81. }, ServerAddress, LocalAddress1);
  82. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(r4);
  83. await TestAsync();
  84. async Task TestAsync()
  85. {
  86. await client.QueryAsync();
  87. Assert.AreEqual(NatType.UnsupportedServer, client.State.NatType);
  88. }
  89. }
  90. [TestMethod]
  91. public async Task NoNatTestAsync()
  92. {
  93. Mock<StunClient3489> mock = new(Any, Any, default!);
  94. StunClient3489 client = mock.Object;
  95. StunResponse openInternetTest1Response = new(
  96. new StunMessage5389
  97. {
  98. Attributes = new[]
  99. {
  100. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  101. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  102. }
  103. },
  104. ServerAddress,
  105. MappedAddress1
  106. );
  107. StunResponse test2Response = new(
  108. new StunMessage5389
  109. {
  110. Attributes = new[]
  111. {
  112. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  113. }
  114. },
  115. ChangedAddress1,
  116. MappedAddress1
  117. );
  118. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(openInternetTest1Response);
  119. mock.Setup(x => x.LocalEndPoint).Returns(MappedAddress1);
  120. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(test2Response);
  121. Assert.AreEqual(NatType.Unknown, client.State.NatType);
  122. await client.QueryAsync();
  123. Assert.AreEqual(NatType.OpenInternet, client.State.NatType);
  124. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  125. await client.QueryAsync();
  126. Assert.AreEqual(NatType.SymmetricUdpFirewall, client.State.NatType);
  127. }
  128. [TestMethod]
  129. public async Task FullConeTestAsync()
  130. {
  131. Mock<StunClient3489> mock = new(Any, Any, default!);
  132. StunClient3489? client = mock.Object;
  133. StunResponse test1Response = new(
  134. new StunMessage5389
  135. {
  136. Attributes = new[]
  137. {
  138. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  139. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  140. }
  141. },
  142. ServerAddress,
  143. LocalAddress1
  144. );
  145. StunResponse fullConeResponse = new(
  146. new StunMessage5389
  147. {
  148. Attributes = new[]
  149. {
  150. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  151. }
  152. },
  153. ChangedAddress1,
  154. LocalAddress1
  155. );
  156. StunResponse unsupportedResponse1 = new(
  157. new StunMessage5389
  158. {
  159. Attributes = new[]
  160. {
  161. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  162. }
  163. },
  164. ServerAddress,
  165. LocalAddress1
  166. );
  167. StunResponse unsupportedResponse2 = new(
  168. new StunMessage5389
  169. {
  170. Attributes = new[]
  171. {
  172. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  173. }
  174. },
  175. new IPEndPoint(ServerAddress.Address, ChangedAddress1.Port),
  176. LocalAddress1
  177. );
  178. StunResponse unsupportedResponse3 = new(
  179. new StunMessage5389
  180. {
  181. Attributes = new[]
  182. {
  183. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port)
  184. }
  185. },
  186. new IPEndPoint(ChangedAddress1.Address, ServerAddress.Port),
  187. LocalAddress1
  188. );
  189. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(test1Response);
  190. mock.Setup(x => x.LocalEndPoint).Returns(LocalAddress1);
  191. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(fullConeResponse);
  192. Assert.AreEqual(NatType.Unknown, client.State.NatType);
  193. await client.QueryAsync();
  194. Assert.AreEqual(NatType.FullCone, client.State.NatType);
  195. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(unsupportedResponse1);
  196. await TestUnsupportedServerAsync();
  197. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(unsupportedResponse2);
  198. await TestUnsupportedServerAsync();
  199. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(unsupportedResponse3);
  200. await TestUnsupportedServerAsync();
  201. async Task TestUnsupportedServerAsync()
  202. {
  203. await client.QueryAsync();
  204. Assert.AreEqual(NatType.UnsupportedServer, client.State.NatType);
  205. }
  206. }
  207. [TestMethod]
  208. public async Task SymmetricTestAsync()
  209. {
  210. Mock<StunClient3489> mock = new(Any, Any, default!);
  211. StunClient3489 client = mock.Object;
  212. StunResponse test1Response = new(
  213. new StunMessage5389
  214. {
  215. Attributes = new[]
  216. {
  217. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  218. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  219. }
  220. },
  221. ServerAddress,
  222. LocalAddress1
  223. );
  224. StunResponse test12Response = new(
  225. new StunMessage5389
  226. {
  227. Attributes = new[]
  228. {
  229. BuildMapping(IpFamily.IPv4, MappedAddress2.Address, (ushort)MappedAddress2.Port),
  230. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  231. }
  232. },
  233. ServerAddress,
  234. LocalAddress1
  235. );
  236. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(test1Response);
  237. mock.Setup(x => x.LocalEndPoint).Returns(LocalAddress1);
  238. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  239. mock.Setup(x => x.Test1_2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  240. Assert.AreEqual(NatType.Unknown, client.State.NatType);
  241. await client.QueryAsync();
  242. Assert.AreEqual(NatType.Unknown, client.State.NatType);
  243. mock.Setup(x => x.Test1_2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(test12Response);
  244. await client.QueryAsync();
  245. Assert.AreEqual(NatType.Symmetric, client.State.NatType);
  246. }
  247. [TestMethod]
  248. public async Task RestrictedConeTestAsync()
  249. {
  250. Mock<StunClient3489> mock = new(Any, Any, default!);
  251. StunClient3489 client = mock.Object;
  252. StunResponse test1Response = new(
  253. new StunMessage5389
  254. {
  255. Attributes = new[]
  256. {
  257. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  258. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  259. }
  260. },
  261. ServerAddress,
  262. LocalAddress1
  263. );
  264. StunResponse test3Response = new(
  265. new StunMessage5389
  266. {
  267. Attributes = new[]
  268. {
  269. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  270. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  271. }
  272. },
  273. ChangedAddress2,
  274. LocalAddress1
  275. );
  276. StunResponse test3ErrorResponse = new(
  277. new StunMessage5389
  278. {
  279. Attributes = new[]
  280. {
  281. BuildMapping(IpFamily.IPv4, MappedAddress1.Address, (ushort)MappedAddress1.Port),
  282. BuildChangeAddress(IpFamily.IPv4, ChangedAddress1.Address, (ushort)ChangedAddress1.Port)
  283. }
  284. },
  285. ServerAddress,
  286. LocalAddress1
  287. );
  288. mock.Setup(x => x.Test1Async(It.IsAny<CancellationToken>())).ReturnsAsync(test1Response);
  289. mock.Setup(x => x.LocalEndPoint).Returns(LocalAddress1);
  290. mock.Setup(x => x.Test2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  291. mock.Setup(x => x.Test1_2Async(It.IsAny<IPEndPoint>(), It.IsAny<CancellationToken>())).ReturnsAsync(test1Response);
  292. mock.Setup(x => x.Test3Async(It.IsAny<CancellationToken>())).ReturnsAsync(test3Response);
  293. Assert.AreEqual(NatType.Unknown, client.State.NatType);
  294. await client.QueryAsync();
  295. Assert.AreEqual(NatType.RestrictedCone, client.State.NatType);
  296. mock.Setup(x => x.Test3Async(It.IsAny<CancellationToken>())).ReturnsAsync(test3ErrorResponse);
  297. await client.QueryAsync();
  298. Assert.AreEqual(NatType.PortRestrictedCone, client.State.NatType);
  299. mock.Setup(x => x.Test3Async(It.IsAny<CancellationToken>())).ReturnsAsync(default(StunResponse?));
  300. await client.QueryAsync();
  301. Assert.AreEqual(NatType.PortRestrictedCone, client.State.NatType);
  302. }
  303. [TestMethod]
  304. public async Task Test1Async()
  305. {
  306. IPAddress ip = await _dnsClient.QueryAsync(Server);
  307. using StunClient3489 client = new(new IPEndPoint(ip, Port), Any);
  308. // test I
  309. StunResponse? response1 = await client.Test1Async(default);
  310. Assert.IsNotNull(response1);
  311. Assert.AreEqual(ip, response1.Remote.Address);
  312. Assert.AreEqual(Port, response1.Remote.Port);
  313. Assert.AreNotEqual(Any, client.LocalEndPoint);
  314. IPEndPoint? mappedAddress = response1.Message.GetMappedAddressAttribute();
  315. IPEndPoint? changedAddress = response1.Message.GetChangedAddressAttribute();
  316. Assert.IsNotNull(mappedAddress);
  317. Assert.IsNotNull(changedAddress);
  318. Assert.AreNotEqual(ip, changedAddress.Address);
  319. Assert.AreNotEqual(Port, changedAddress.Port);
  320. // Test I(#2)
  321. StunResponse? response12 = await client.Test1_2Async(changedAddress, default);
  322. Assert.IsNotNull(response12);
  323. Assert.AreEqual(changedAddress.Address, response12.Remote.Address);
  324. Assert.AreEqual(changedAddress.Port, response12.Remote.Port);
  325. }
  326. #if FullCone
  327. [TestMethod]
  328. #endif
  329. public async Task Test2Async()
  330. {
  331. IPAddress ip = await _dnsClient.QueryAsync(Server);
  332. using StunClient3489 client = new(new IPEndPoint(ip, Port), Any);
  333. StunResponse? response2 = await client.Test2Async(ip.AddressFamily is AddressFamily.InterNetworkV6 ? IPv6Any : Any, default);
  334. Assert.IsNotNull(response2);
  335. Assert.AreNotEqual(ip, response2.Remote.Address);
  336. Assert.AreNotEqual(Port, response2.Remote.Port);
  337. }
  338. #if FullCone
  339. [TestMethod]
  340. #endif
  341. public async Task Test3Async()
  342. {
  343. IPAddress ip = await _dnsClient.QueryAsync(Server);
  344. using StunClient3489 client = new(new IPEndPoint(ip, Port), Any);
  345. StunResponse? response = await client.Test3Async(default);
  346. Assert.IsNotNull(response);
  347. Assert.AreEqual(ip, response.Remote.Address);
  348. Assert.AreNotEqual(Port, response.Remote.Port);
  349. }
  350. }