NetworkParseTests.cs 20 KB

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