StunClient5389TCPTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using Dns.Net.Clients;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. using Moq.Protected;
  5. using STUN;
  6. using STUN.Client;
  7. using STUN.Enums;
  8. using STUN.Proxy;
  9. using STUN.StunResult;
  10. using System.Net;
  11. namespace UnitTest;
  12. [TestClass]
  13. public class StunClient5389TCPTest
  14. {
  15. private readonly DefaultDnsClient _dnsClient = new();
  16. private static readonly IPEndPoint Any = new(IPAddress.Any, 0);
  17. private static readonly IPEndPoint LocalAddress1 = IPEndPoint.Parse(@"127.0.0.1:114");
  18. private static readonly IPEndPoint MappedAddress1 = IPEndPoint.Parse(@"1.1.1.1:114");
  19. private static readonly IPEndPoint MappedAddress2 = IPEndPoint.Parse(@"1.1.1.1:514");
  20. private static readonly IPEndPoint ServerAddress = IPEndPoint.Parse(@"2.2.2.2:1919");
  21. private static readonly IPEndPoint ChangedAddress1 = IPEndPoint.Parse(@"3.3.3.3:23333");
  22. private static readonly IPEndPoint ChangedAddress2 = IPEndPoint.Parse(@"2.2.2.2:810");
  23. private static readonly IPEndPoint ChangedAddress3 = IPEndPoint.Parse(@"3.3.3.3:1919");
  24. [TestMethod]
  25. public async Task BindingTestSuccessAsync()
  26. {
  27. IPAddress ip = await _dnsClient.QueryAsync(@"stun.hot-chilli.net");
  28. using IStunClient5389 client = new StunClient5389TCP(new IPEndPoint(ip, StunServer.DefaultPort), Any);
  29. StunResult5389 response = await client.BindingTestAsync();
  30. Assert.AreEqual(BindingTestResult.Success, response.BindingTestResult);
  31. Assert.AreEqual(MappingBehavior.Unknown, response.MappingBehavior);
  32. Assert.AreEqual(FilteringBehavior.Unknown, response.FilteringBehavior);
  33. Assert.IsNotNull(response.PublicEndPoint);
  34. Assert.IsNotNull(response.LocalEndPoint);
  35. Assert.IsNotNull(response.OtherEndPoint);
  36. }
  37. [TestMethod]
  38. public async Task BindingTestFailAsync()
  39. {
  40. IPAddress ip = IPAddress.Parse(@"1.1.1.1");
  41. using IStunClient5389 client = new StunClient5389TCP(new IPEndPoint(ip, StunServer.DefaultPort), Any);
  42. StunResult5389 response = await client.BindingTestAsync();
  43. Assert.AreEqual(BindingTestResult.Fail, response.BindingTestResult);
  44. Assert.AreEqual(MappingBehavior.Unknown, response.MappingBehavior);
  45. Assert.AreEqual(FilteringBehavior.Unknown, response.FilteringBehavior);
  46. Assert.IsNull(response.PublicEndPoint);
  47. Assert.IsNull(response.LocalEndPoint);
  48. Assert.IsNull(response.OtherEndPoint);
  49. }
  50. [TestMethod]
  51. public async Task TlsBindingTestSuccessAsync()
  52. {
  53. Assert.IsTrue(StunServer.TryParse(@"stun.fitauto.ru", out StunServer? stunServer, StunServer.DefaultTlsPort));
  54. IPAddress ip = await _dnsClient.QueryAsync(stunServer.Hostname);
  55. ITcpProxy tls = new TlsProxy(stunServer.Hostname);
  56. using IStunClient5389 client = new StunClient5389TCP(new IPEndPoint(ip, StunServer.DefaultPort), Any, tls);
  57. StunResult5389 response = await client.BindingTestAsync();
  58. Assert.AreEqual(BindingTestResult.Success, response.BindingTestResult);
  59. Assert.AreEqual(MappingBehavior.Unknown, response.MappingBehavior);
  60. Assert.AreEqual(FilteringBehavior.Unknown, response.FilteringBehavior);
  61. Assert.IsNotNull(response.PublicEndPoint);
  62. Assert.IsNotNull(response.LocalEndPoint);
  63. Assert.IsNotNull(response.OtherEndPoint);
  64. }
  65. [Ignore]
  66. [TestMethod]
  67. public async Task TestServerAsync()
  68. {
  69. const string url = @"https://raw.githubusercontent.com/pradt2/always-online-stun/master/valid_hosts_tcp.txt";
  70. HttpClient httpClient = new();
  71. string listRaw = await httpClient.GetStringAsync(url);
  72. string[] list = listRaw.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
  73. foreach (string host in list)
  74. {
  75. try
  76. {
  77. if (!HostnameEndpoint.TryParse(host, out HostnameEndpoint? hostEndpoint, StunServer.DefaultPort))
  78. {
  79. continue;
  80. }
  81. IPAddress ip = await _dnsClient.QueryAsync(hostEndpoint.Hostname);
  82. using IStunClient5389 client = new StunClient5389TCP(new IPEndPoint(ip, hostEndpoint.Port), Any);
  83. await client.QueryAsync();
  84. if (client.State.MappingBehavior is MappingBehavior.AddressAndPortDependent or MappingBehavior.AddressDependent or MappingBehavior.EndpointIndependent or MappingBehavior.Direct)
  85. {
  86. Console.WriteLine(host);
  87. }
  88. }
  89. catch
  90. {
  91. // ignored
  92. }
  93. }
  94. }
  95. [Ignore]
  96. [TestMethod]
  97. public async Task TestTlsServerAsync()
  98. {
  99. const string url = @"https://raw.githubusercontent.com/pradt2/always-online-stun/master/valid_hosts_tcp.txt";
  100. HttpClient httpClient = new();
  101. string listRaw = await httpClient.GetStringAsync(url);
  102. string[] list = listRaw.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
  103. foreach (string host in list)
  104. {
  105. try
  106. {
  107. if (!HostnameEndpoint.TryParse(host, out HostnameEndpoint? hostEndpoint, StunServer.DefaultTlsPort))
  108. {
  109. continue;
  110. }
  111. IPAddress ip = await _dnsClient.QueryAsync(hostEndpoint.Hostname);
  112. ITcpProxy proxy = new TlsProxy(hostEndpoint.Hostname);
  113. using IStunClient5389 client = new StunClient5389TCP(new IPEndPoint(ip, StunServer.DefaultTlsPort), Any, proxy);
  114. await client.QueryAsync();
  115. if (client.State.MappingBehavior is MappingBehavior.AddressAndPortDependent or MappingBehavior.AddressDependent or MappingBehavior.EndpointIndependent or MappingBehavior.Direct)
  116. {
  117. Console.WriteLine(host);
  118. }
  119. }
  120. catch
  121. {
  122. // ignored
  123. }
  124. }
  125. }
  126. [TestMethod]
  127. public async Task MappingBehaviorTestFailAsync()
  128. {
  129. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  130. IStunClient5389 client = mock.Object;
  131. StunResult5389 fail = new() { BindingTestResult = BindingTestResult.Fail };
  132. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(fail);
  133. await client.QueryAsync();
  134. Assert.AreEqual(BindingTestResult.Fail, client.State.BindingTestResult);
  135. Assert.AreEqual(MappingBehavior.Unknown, client.State.MappingBehavior);
  136. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  137. Assert.IsNull(client.State.PublicEndPoint);
  138. Assert.IsNull(client.State.LocalEndPoint);
  139. Assert.IsNull(client.State.OtherEndPoint);
  140. }
  141. [TestMethod]
  142. public async Task MappingBehaviorTestUnsupportedServerAsync()
  143. {
  144. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  145. IStunClient5389 client = mock.Object;
  146. StunResult5389 r1 = new()
  147. {
  148. BindingTestResult = BindingTestResult.Success,
  149. PublicEndPoint = MappedAddress1,
  150. LocalEndPoint = LocalAddress1
  151. };
  152. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  153. await TestAsync();
  154. StunResult5389 r2 = new()
  155. {
  156. BindingTestResult = BindingTestResult.Success,
  157. PublicEndPoint = MappedAddress1,
  158. LocalEndPoint = LocalAddress1,
  159. OtherEndPoint = ChangedAddress2
  160. };
  161. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
  162. await TestAsync();
  163. StunResult5389 r3 = new()
  164. {
  165. BindingTestResult = BindingTestResult.Success,
  166. PublicEndPoint = MappedAddress1,
  167. LocalEndPoint = LocalAddress1,
  168. OtherEndPoint = ChangedAddress3
  169. };
  170. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
  171. await TestAsync();
  172. return;
  173. async Task TestAsync()
  174. {
  175. await client.QueryAsync();
  176. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  177. Assert.AreEqual(MappingBehavior.UnsupportedServer, client.State.MappingBehavior);
  178. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  179. Assert.IsNotNull(client.State.PublicEndPoint);
  180. Assert.IsNotNull(client.State.LocalEndPoint);
  181. }
  182. }
  183. [TestMethod]
  184. public async Task MappingBehaviorTestDirectAsync()
  185. {
  186. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  187. IStunClient5389 client = mock.Object;
  188. StunResult5389 response = new()
  189. {
  190. BindingTestResult = BindingTestResult.Success,
  191. PublicEndPoint = MappedAddress1,
  192. LocalEndPoint = MappedAddress1,
  193. OtherEndPoint = ChangedAddress1
  194. };
  195. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(response);
  196. await client.QueryAsync();
  197. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  198. Assert.AreEqual(MappingBehavior.Direct, client.State.MappingBehavior);
  199. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  200. Assert.IsNotNull(client.State.PublicEndPoint);
  201. Assert.IsNotNull(client.State.LocalEndPoint);
  202. Assert.IsNotNull(client.State.OtherEndPoint);
  203. }
  204. [TestMethod]
  205. public async Task MappingBehaviorTestEndpointIndependentAsync()
  206. {
  207. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  208. IStunClient5389 client = mock.Object;
  209. StunResult5389 r1 = new()
  210. {
  211. BindingTestResult = BindingTestResult.Success,
  212. PublicEndPoint = MappedAddress1,
  213. LocalEndPoint = LocalAddress1,
  214. OtherEndPoint = ChangedAddress1
  215. };
  216. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ItExpr.IsAny<IPEndPoint>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  217. await client.QueryAsync();
  218. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  219. Assert.AreEqual(MappingBehavior.EndpointIndependent, client.State.MappingBehavior);
  220. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  221. Assert.IsNotNull(client.State.PublicEndPoint);
  222. Assert.IsNotNull(client.State.LocalEndPoint);
  223. Assert.IsNotNull(client.State.OtherEndPoint);
  224. }
  225. [TestMethod]
  226. public async Task MappingBehaviorTest2FailAsync()
  227. {
  228. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  229. IStunClient5389 client = mock.Object;
  230. StunResult5389 r1 = new()
  231. {
  232. BindingTestResult = BindingTestResult.Success,
  233. PublicEndPoint = MappedAddress1,
  234. LocalEndPoint = LocalAddress1,
  235. OtherEndPoint = ChangedAddress1
  236. };
  237. StunResult5389 r2 = new() { BindingTestResult = BindingTestResult.Fail };
  238. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  239. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
  240. await client.QueryAsync();
  241. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  242. Assert.AreEqual(MappingBehavior.Fail, client.State.MappingBehavior);
  243. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  244. Assert.IsNotNull(client.State.PublicEndPoint);
  245. Assert.IsNotNull(client.State.LocalEndPoint);
  246. Assert.IsNotNull(client.State.OtherEndPoint);
  247. }
  248. [TestMethod]
  249. public async Task MappingBehaviorTestAddressDependentAsync()
  250. {
  251. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  252. IStunClient5389 client = mock.Object;
  253. StunResult5389 r1 = new()
  254. {
  255. BindingTestResult = BindingTestResult.Success,
  256. PublicEndPoint = MappedAddress1,
  257. LocalEndPoint = LocalAddress1,
  258. OtherEndPoint = ChangedAddress1
  259. };
  260. StunResult5389 r2 = new()
  261. {
  262. BindingTestResult = BindingTestResult.Success,
  263. PublicEndPoint = MappedAddress2,
  264. LocalEndPoint = LocalAddress1,
  265. OtherEndPoint = ChangedAddress1
  266. };
  267. StunResult5389 r3 = new()
  268. {
  269. BindingTestResult = BindingTestResult.Success,
  270. PublicEndPoint = MappedAddress2,
  271. LocalEndPoint = LocalAddress1,
  272. OtherEndPoint = ChangedAddress1
  273. };
  274. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  275. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
  276. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress1, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
  277. await client.QueryAsync();
  278. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  279. Assert.AreEqual(MappingBehavior.AddressDependent, client.State.MappingBehavior);
  280. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  281. Assert.IsNotNull(client.State.PublicEndPoint);
  282. Assert.IsNotNull(client.State.LocalEndPoint);
  283. Assert.IsNotNull(client.State.OtherEndPoint);
  284. }
  285. [TestMethod]
  286. public async Task MappingBehaviorTestAddressAndPortDependentAsync()
  287. {
  288. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  289. IStunClient5389 client = mock.Object;
  290. StunResult5389 r1 = new()
  291. {
  292. BindingTestResult = BindingTestResult.Success,
  293. PublicEndPoint = MappedAddress1,
  294. LocalEndPoint = LocalAddress1,
  295. OtherEndPoint = ChangedAddress1
  296. };
  297. StunResult5389 r2 = new()
  298. {
  299. BindingTestResult = BindingTestResult.Success,
  300. PublicEndPoint = MappedAddress2,
  301. LocalEndPoint = LocalAddress1,
  302. OtherEndPoint = ChangedAddress1
  303. };
  304. StunResult5389 r3 = new()
  305. {
  306. BindingTestResult = BindingTestResult.Success,
  307. PublicEndPoint = MappedAddress1,
  308. LocalEndPoint = LocalAddress1,
  309. OtherEndPoint = ChangedAddress1
  310. };
  311. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  312. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
  313. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress1, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
  314. await client.QueryAsync();
  315. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  316. Assert.AreEqual(MappingBehavior.AddressAndPortDependent, client.State.MappingBehavior);
  317. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  318. Assert.IsNotNull(client.State.PublicEndPoint);
  319. Assert.IsNotNull(client.State.LocalEndPoint);
  320. Assert.IsNotNull(client.State.OtherEndPoint);
  321. }
  322. [TestMethod]
  323. public async Task MappingBehaviorTest3FailAsync()
  324. {
  325. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  326. IStunClient5389 client = mock.Object;
  327. StunResult5389 r1 = new()
  328. {
  329. BindingTestResult = BindingTestResult.Success,
  330. PublicEndPoint = MappedAddress1,
  331. LocalEndPoint = LocalAddress1,
  332. OtherEndPoint = ChangedAddress1
  333. };
  334. StunResult5389 r2 = new()
  335. {
  336. BindingTestResult = BindingTestResult.Success,
  337. PublicEndPoint = MappedAddress2,
  338. LocalEndPoint = LocalAddress1,
  339. OtherEndPoint = ChangedAddress1
  340. };
  341. StunResult5389 r3 = new() { BindingTestResult = BindingTestResult.Fail };
  342. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ServerAddress, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r1);
  343. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress3, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r2);
  344. mock.Protected().Setup<ValueTask<StunResult5389>>(@"BindingTestBaseAsync", ChangedAddress1, ItExpr.IsAny<CancellationToken>()).ReturnsAsync(r3);
  345. await client.QueryAsync();
  346. Assert.AreEqual(BindingTestResult.Success, client.State.BindingTestResult);
  347. Assert.AreEqual(MappingBehavior.Fail, client.State.MappingBehavior);
  348. Assert.AreEqual(FilteringBehavior.None, client.State.FilteringBehavior);
  349. Assert.IsNotNull(client.State.PublicEndPoint);
  350. Assert.IsNotNull(client.State.LocalEndPoint);
  351. Assert.IsNotNull(client.State.OtherEndPoint);
  352. }
  353. [TestMethod]
  354. public async Task FilteringBehaviorTestAsync()
  355. {
  356. Mock<StunClient5389TCP> mock = new(ServerAddress, Any, default!);
  357. IStunClient5389 client = mock.Object;
  358. await Assert.ThrowsExceptionAsync<NotSupportedException>(async () => await client.FilteringBehaviorTestAsync());
  359. }
  360. }