UnitTesting.cs 20 KB

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