UnitTesting.cs 19 KB

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