StunClient3489Test.cs 14 KB

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