NetworkParseTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. using System;
  2. using System.Net;
  3. using Emby.Dlna.PlayTo;
  4. using Jellyfin.Networking.Configuration;
  5. using Jellyfin.Networking.Manager;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Net;
  8. using Moq;
  9. using Microsoft.Extensions.Logging.Abstractions;
  10. using Xunit;
  11. using System.Collections.ObjectModel;
  12. namespace Jellyfin.Networking.Tests
  13. {
  14. public class NetworkParseTests
  15. {
  16. /// <summary>
  17. /// Tries to identify the string and return an object of that class.
  18. /// </summary>
  19. /// <param name="addr">String to parse.</param>
  20. /// <param name="result">IPObject to return.</param>
  21. /// <returns>True if the value parsed successfully.</returns>
  22. private static bool TryParse(string addr, out IPObject result)
  23. {
  24. if (!string.IsNullOrEmpty(addr))
  25. {
  26. // Is it an IP address
  27. if (IPNetAddress.TryParse(addr, out IPNetAddress nw))
  28. {
  29. result = nw;
  30. return true;
  31. }
  32. if (IPHost.TryParse(addr, out IPHost h))
  33. {
  34. result = h;
  35. return true;
  36. }
  37. }
  38. result = IPNetAddress.None;
  39. return false;
  40. }
  41. private static IConfigurationManager GetMockConfig(NetworkConfiguration conf)
  42. {
  43. var configManager = new Mock<IConfigurationManager>
  44. {
  45. CallBase = true
  46. };
  47. configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).Returns(conf);
  48. return (IConfigurationManager)configManager.Object;
  49. }
  50. [Theory]
  51. [InlineData("192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24]")]
  52. [InlineData("192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
  53. [InlineData("192.168.1.208/24,-16,vEthernet1:192.168.1.208/24,-16,vEthernet212;200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
  54. public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
  55. {
  56. var conf = new NetworkConfiguration()
  57. {
  58. EnableIPV6 = true,
  59. EnableIPV4 = true,
  60. LocalNetworkSubnets = lan?.Split(';') ?? throw new ArgumentNullException(nameof(lan))
  61. };
  62. NetworkManager.MockNetworkSettings = interfaces;
  63. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  64. NetworkManager.MockNetworkSettings = string.Empty;
  65. Assert.True(string.Equals(nm.GetInternalBindAddresses().AsString(), value, StringComparison.Ordinal));
  66. }
  67. [Theory]
  68. [InlineData("192.168.10.0/24, !192.168.10.60/32", "192.168.10.60")]
  69. public void TextIsInNetwork(string network, string value)
  70. {
  71. if (network == null)
  72. {
  73. throw new ArgumentNullException(nameof(network));
  74. }
  75. var conf = new NetworkConfiguration()
  76. {
  77. EnableIPV6 = true,
  78. EnableIPV4 = true,
  79. LocalNetworkSubnets = network.Split(',')
  80. };
  81. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  82. Assert.True(!nm.IsInLocalNetwork(value));
  83. }
  84. [Theory]
  85. [InlineData("127.0.0.1")]
  86. [InlineData("127.0.0.1:123")]
  87. [InlineData("localhost")]
  88. [InlineData("localhost:1345")]
  89. [InlineData("www.google.co.uk")]
  90. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
  91. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
  92. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]:124")]
  93. [InlineData("fe80::7add:12ff:febb:c67b%16")]
  94. [InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
  95. [InlineData("192.168.1.2/255.255.255.0")]
  96. [InlineData("192.168.1.2/24")]
  97. public void TestCollectionCreation(string address)
  98. {
  99. Assert.True(TryParse(address, out _));
  100. }
  101. [Theory]
  102. [InlineData("256.128.0.0.0.1")]
  103. [InlineData("127.0.0.1#")]
  104. [InlineData("localhost!")]
  105. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
  106. public void TestInvalidCollectionCreation(string address)
  107. {
  108. Assert.False(TryParse(address, out _));
  109. }
  110. [Theory]
  111. [InlineData("127.0.0.1#",
  112. "[]",
  113. "[]",
  114. "[]",
  115. "[]",
  116. "[]")]
  117. [InlineData("[127.0.0.1]",
  118. "[]",
  119. "[]",
  120. "[127.0.0.1/32]",
  121. "[127.0.0.1/32]",
  122. "[]")]
  123. [InlineData("",
  124. "[]",
  125. "[]",
  126. "[]",
  127. "[]",
  128. "[]")]
  129. [InlineData("192.158.1.2/255.255.0.0,192.169.1.2/8",
  130. "[192.158.1.2/16,192.169.1.2/8]",
  131. "[192.158.1.2/16,192.169.1.2/8]",
  132. "[]",
  133. "[]",
  134. "[192.158.0.0/16,192.0.0.0/8]")]
  135. [InlineData("192.158.1.2/16, localhost, fd23:184f:2029:0:3139:7386:67d7:d517, [10.10.10.10]",
  136. "[192.158.1.2/16,127.0.0.1/32,fd23:184f:2029:0:3139:7386:67d7:d517/128]",
  137. "[192.158.1.2/16,127.0.0.1/32]",
  138. "[10.10.10.10/32]",
  139. "[10.10.10.10/32]",
  140. "[192.158.0.0/16,127.0.0.1/32,fd23:184f:2029:0:3139:7386:67d7:d517/128]")]
  141. public void TestCollections(string settings, string result1, string result2, string result3, string result4, string result5)
  142. {
  143. if (settings == null)
  144. {
  145. throw new ArgumentNullException(nameof(settings));
  146. }
  147. var conf = new NetworkConfiguration()
  148. {
  149. EnableIPV6 = true,
  150. EnableIPV4 = true,
  151. };
  152. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  153. // Test included, IP6.
  154. Collection<IPObject> nc = nm.CreateIPCollection(settings.Split(","), false);
  155. Assert.True(string.Equals(nc?.AsString(), result1, System.StringComparison.OrdinalIgnoreCase));
  156. // Test excluded, non IP6.
  157. nc = nm.CreateIPCollection(settings.Split(","), true);
  158. Assert.True(string.Equals(nc?.AsString(), result3, System.StringComparison.OrdinalIgnoreCase));
  159. conf.EnableIPV6 = false;
  160. nm.UpdateSettings(conf);
  161. // Test included, non IP6.
  162. nc = nm.CreateIPCollection(settings.Split(","), false);
  163. Assert.True(string.Equals(nc?.AsString(), result2, System.StringComparison.OrdinalIgnoreCase));
  164. // Test excluded, including IPv6.
  165. nc = nm.CreateIPCollection(settings.Split(","), true);
  166. Assert.True(string.Equals(nc?.AsString(), result4, System.StringComparison.OrdinalIgnoreCase));
  167. conf.EnableIPV6 = true;
  168. nm.UpdateSettings(conf);
  169. // Test network addresses of collection.
  170. nc = nm.CreateIPCollection(settings.Split(","), false);
  171. nc = nc.AsNetworks();
  172. Assert.True(string.Equals(nc?.AsString(), result5, System.StringComparison.OrdinalIgnoreCase));
  173. }
  174. [Theory]
  175. [InlineData("127.0.0.1", "fd23:184f:2029:0:3139:7386:67d7:d517/64,fd23:184f:2029:0:c0f0:8a8a:7605:fffa/128,fe80::3139:7386:67d7:d517%16/64,192.168.1.208/24,::1/128,127.0.0.1/8", "[127.0.0.1/32]")]
  176. [InlineData("127.0.0.1", "127.0.0.1/8", "[127.0.0.1/32]")]
  177. public void UnionCheck(string settings, string compare, string result)
  178. {
  179. if (settings == null)
  180. {
  181. throw new ArgumentNullException(nameof(settings));
  182. }
  183. if (compare == null)
  184. {
  185. throw new ArgumentNullException(nameof(compare));
  186. }
  187. if (result == null)
  188. {
  189. throw new ArgumentNullException(nameof(result));
  190. }
  191. var conf = new NetworkConfiguration()
  192. {
  193. EnableIPV6 = true,
  194. EnableIPV4 = true,
  195. };
  196. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  197. Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(","), false);
  198. Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(","), false);
  199. Assert.True(nc1.Union(nc2).AsString() == result);
  200. }
  201. [Theory]
  202. [InlineData("192.168.5.85/24", "192.168.5.1")]
  203. [InlineData("192.168.5.85/24", "192.168.5.254")]
  204. [InlineData("10.128.240.50/30", "10.128.240.48")]
  205. [InlineData("10.128.240.50/30", "10.128.240.49")]
  206. [InlineData("10.128.240.50/30", "10.128.240.50")]
  207. [InlineData("10.128.240.50/30", "10.128.240.51")]
  208. [InlineData("127.0.0.1/8", "127.0.0.1")]
  209. public void IpV4SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  210. {
  211. var ipAddressObj = IPNetAddress.Parse(netMask);
  212. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  213. }
  214. [Theory]
  215. [InlineData("192.168.5.85/24", "192.168.4.254")]
  216. [InlineData("192.168.5.85/24", "191.168.5.254")]
  217. [InlineData("10.128.240.50/30", "10.128.240.47")]
  218. [InlineData("10.128.240.50/30", "10.128.240.52")]
  219. [InlineData("10.128.240.50/30", "10.128.239.50")]
  220. [InlineData("10.128.240.50/30", "10.127.240.51")]
  221. public void IpV4SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  222. {
  223. var ipAddressObj = IPNetAddress.Parse(netMask);
  224. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  225. }
  226. [Theory]
  227. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  228. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
  229. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
  230. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
  231. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  232. public void IpV6SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  233. {
  234. var ipAddressObj = IPNetAddress.Parse(netMask);
  235. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  236. }
  237. [Theory]
  238. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
  239. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
  240. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
  241. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
  242. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
  243. public void IpV6SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  244. {
  245. var ipAddressObj = IPNetAddress.Parse(netMask);
  246. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  247. }
  248. [Theory]
  249. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1/32")]
  250. [InlineData("10.0.0.0/8", "10.10.10.1/32")]
  251. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1")]
  252. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1/32")]
  253. [InlineData("10.10.0.0/16", "10.10.10.1/32")]
  254. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1")]
  255. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1/32")]
  256. [InlineData("10.10.10.0/24", "10.10.10.1/32")]
  257. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1")]
  258. public void TestSubnets(string network, string ip)
  259. {
  260. Assert.True(TryParse(network, out IPObject? networkObj));
  261. Assert.True(TryParse(ip, out IPObject? ipObj));
  262. Assert.True(networkObj.Contains(ipObj));
  263. }
  264. [Theory]
  265. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24", "172.168.1.2/24")]
  266. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24, 10.10.10.1", "172.168.1.2/24,10.10.10.1/24")]
  267. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/255.255.255.0, 10.10.10.1", "192.168.1.2/24,10.10.10.1/24")]
  268. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/24, 100.10.10.1", "192.168.1.2/24")]
  269. [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "194.168.1.2/24, 100.10.10.1", "")]
  270. public void TestMatches(string source, string dest, string result)
  271. {
  272. if (source == null)
  273. {
  274. throw new ArgumentNullException(nameof(source));
  275. }
  276. if (dest == null)
  277. {
  278. throw new ArgumentNullException(nameof(dest));
  279. }
  280. if (result == null)
  281. {
  282. throw new ArgumentNullException(nameof(result));
  283. }
  284. var conf = new NetworkConfiguration()
  285. {
  286. EnableIPV6 = true,
  287. EnableIPV4 = true
  288. };
  289. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  290. // Test included, IP6.
  291. Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(","));
  292. Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(","));
  293. Collection<IPObject> ncResult = ncSource.Union(ncDest);
  294. Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(","));
  295. Assert.True(ncResult.Compare(resultCollection));
  296. }
  297. [Theory]
  298. [InlineData("10.1.1.1/32", "10.1.1.1")]
  299. [InlineData("192.168.1.254/32", "192.168.1.254/255.255.255.255")]
  300. public void TestEquals(string source, string dest)
  301. {
  302. Assert.True(IPNetAddress.Parse(source).Equals(IPNetAddress.Parse(dest)));
  303. Assert.True(IPNetAddress.Parse(dest).Equals(IPNetAddress.Parse(source)));
  304. }
  305. [Theory]
  306. // Testing bind interfaces. These are set for my system so won't work elsewhere.
  307. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  308. //
  309. // This test is to replicate how DNLA requests work throughout the system.
  310. // User on internal network, we're bound internal and external - so result is internal.
  311. [InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
  312. // User on external network, we're bound internal and external - so result is external.
  313. [InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
  314. // User on internal network, we're bound internal only - so result is internal.
  315. [InlineData("10.10.10.10", "eth16", false, "eth16")]
  316. // User on internal network, no binding specified - so result is the 1st internal.
  317. [InlineData("192.168.1.1", "", false, "eth16")]
  318. // User on external network, internal binding only - so result is the 1st internal.
  319. [InlineData("jellyfin.org", "eth16", false, "eth16")]
  320. // User on external network, no binding - so result is the 1st external.
  321. [InlineData("jellyfin.org", "", false, "eth11")]
  322. // User assumed to be internal, no binding - so result is the 1st internal.
  323. [InlineData("", "", false, "eth16")]
  324. public void TestBindInterfaces(string source, string bindAddresses, bool ipv6enabled, string result)
  325. {
  326. if (source == null)
  327. {
  328. throw new ArgumentNullException(nameof(source));
  329. }
  330. if (bindAddresses == null)
  331. {
  332. throw new ArgumentNullException(nameof(bindAddresses));
  333. }
  334. if (result == null)
  335. {
  336. throw new ArgumentNullException(nameof(result));
  337. }
  338. var conf = new NetworkConfiguration()
  339. {
  340. LocalNetworkAddresses = bindAddresses.Split(','),
  341. EnableIPV6 = ipv6enabled,
  342. EnableIPV4 = true
  343. };
  344. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11";
  345. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  346. NetworkManager.MockNetworkSettings = string.Empty;
  347. _ = nm.TryParseInterface(result, out Collection<IPObject>? resultObj);
  348. if (resultObj != null)
  349. {
  350. result = ((IPNetAddress)resultObj[0]).ToString(true);
  351. var intf = nm.GetBindInterface(source, out int? _);
  352. Assert.True(string.Equals(intf, result, System.StringComparison.OrdinalIgnoreCase));
  353. }
  354. }
  355. [Theory]
  356. // Testing bind interfaces. These are set for my system so won't work elsewhere.
  357. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  358. //
  359. // This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
  360. // User on internal network, we're bound internal and external - so result is internal override.
  361. [InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
  362. // User on external network, we're bound internal and external - so result is override.
  363. [InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  364. // User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
  365. [InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
  366. // User on internal network, no binding specified - so result is the 1st internal.
  367. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  368. // User on external network, internal binding only - so asumption is a proxy forward, return external override.
  369. [InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  370. // User on external network, no binding - so result is the 1st external which is overriden.
  371. [InlineData("jellyfin.org", "192.168.1.0/24", "", false, "0.0.0.0 = http://helloworld.com", "http://helloworld.com")]
  372. // User assumed to be internal, no binding - so result is the 1st internal.
  373. [InlineData("", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  374. // User is internal, no binding - so result is the 1st internal, which is then overridden.
  375. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
  376. public void TestBindInterfaceOverrides(string source, string lan, string bindAddresses, bool ipv6enabled, string publishedServers, string result)
  377. {
  378. if (lan == null)
  379. {
  380. throw new ArgumentNullException(nameof(lan));
  381. }
  382. if (bindAddresses == null)
  383. {
  384. throw new ArgumentNullException(nameof(bindAddresses));
  385. }
  386. var conf = new NetworkConfiguration()
  387. {
  388. LocalNetworkSubnets = lan.Split(','),
  389. LocalNetworkAddresses = bindAddresses.Split(','),
  390. EnableIPV6 = ipv6enabled,
  391. EnableIPV4 = true,
  392. PublishedServerUriBySubnet = new string[] { publishedServers }
  393. };
  394. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11";
  395. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  396. NetworkManager.MockNetworkSettings = string.Empty;
  397. if (nm.TryParseInterface(result, out Collection<IPObject>? resultObj) && resultObj != null)
  398. {
  399. // Parse out IPAddresses so we can do a string comparison. (Ignore subnet masks).
  400. result = ((IPNetAddress)resultObj[0]).ToString(true);
  401. }
  402. var intf = nm.GetBindInterface(source, out int? _);
  403. Assert.Equal(intf, result);
  404. }
  405. }
  406. }