NetworkParseTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Net;
  5. using Jellyfin.Networking.Configuration;
  6. using Jellyfin.Networking.Manager;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Common.Net;
  9. using Microsoft.Extensions.Logging.Abstractions;
  10. using Moq;
  11. using Xunit;
  12. namespace Jellyfin.Networking.Tests
  13. {
  14. public class NetworkParseTests
  15. {
  16. internal static IConfigurationManager GetMockConfig(NetworkConfiguration conf)
  17. {
  18. var configManager = new Mock<IConfigurationManager>
  19. {
  20. CallBase = true
  21. };
  22. configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).Returns(conf);
  23. return (IConfigurationManager)configManager.Object;
  24. }
  25. /// <summary>
  26. /// Checks the ability to ignore virtual interfaces.
  27. /// </summary>
  28. /// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param>
  29. /// <param name="lan">LAN addresses.</param>
  30. /// <param name="value">Bind addresses that are excluded.</param>
  31. [Theory]
  32. // All valid
  33. [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]")]
  34. // eth16 only
  35. [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]")]
  36. // All interfaces excluded. (including loopbacks)
  37. [InlineData("192.168.1.208/24,-16,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[127.0.0.1/8,::1/128]")]
  38. // vEthernet1 and vEthernet212 should be excluded.
  39. [InlineData("192.168.1.200/24,-20,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.200/24", "[200.200.200.200/24,127.0.0.1/8,::1/128]")]
  40. // Overlapping interface,
  41. [InlineData("192.168.1.110/24,-20,br0|192.168.1.10/24,-16,br0|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.110/24,192.168.1.10/24]")]
  42. public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
  43. {
  44. var conf = new NetworkConfiguration()
  45. {
  46. EnableIPV6 = true,
  47. EnableIPV4 = true,
  48. LocalNetworkSubnets = lan?.Split(';') ?? throw new ArgumentNullException(nameof(lan))
  49. };
  50. NetworkManager.MockNetworkSettings = interfaces;
  51. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  52. NetworkManager.MockNetworkSettings = string.Empty;
  53. Assert.Equal(nm.GetInternalBindAddresses().AsString(), value);
  54. }
  55. /// <summary>
  56. /// Checks IP address formats.
  57. /// </summary>
  58. /// <param name="address">IP Address.</param>
  59. [Theory]
  60. [InlineData("127.0.0.1")]
  61. [InlineData("127.0.0.1:123")]
  62. [InlineData("localhost")]
  63. [InlineData("localhost:1345")]
  64. [InlineData("www.google.co.uk")]
  65. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
  66. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
  67. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]:124")]
  68. [InlineData("fe80::7add:12ff:febb:c67b%16")]
  69. [InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
  70. [InlineData("fe80::7add:12ff:febb:c67b%16:123")]
  71. [InlineData("[fe80::7add:12ff:febb:c67b%16]")]
  72. [InlineData("192.168.1.2/255.255.255.0")]
  73. [InlineData("192.168.1.2/24")]
  74. public void ValidHostStrings(string address)
  75. {
  76. Assert.True(IPHost.TryParse(address, out _));
  77. }
  78. /// <summary>
  79. /// Checks IP address formats.
  80. /// </summary>
  81. /// <param name="address">IP Address.</param>
  82. [Theory]
  83. [InlineData("127.0.0.1")]
  84. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
  85. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
  86. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
  87. [InlineData("fe80::7add:12ff:febb:c67b%16")]
  88. [InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
  89. [InlineData("fe80::7add:12ff:febb:c67b%16:123")]
  90. [InlineData("[fe80::7add:12ff:febb:c67b%16]")]
  91. [InlineData("192.168.1.2/255.255.255.0")]
  92. [InlineData("192.168.1.2/24")]
  93. public void ValidIPStrings(string address)
  94. {
  95. Assert.True(IPNetAddress.TryParse(address, out _));
  96. }
  97. /// <summary>
  98. /// All should be invalid address strings.
  99. /// </summary>
  100. /// <param name="address">Invalid address strings.</param>
  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. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
  107. public void InvalidAddressString(string address)
  108. {
  109. Assert.False(IPNetAddress.TryParse(address, out _));
  110. Assert.False(IPHost.TryParse(address, out _));
  111. }
  112. /// <summary>
  113. /// Test collection parsing.
  114. /// </summary>
  115. /// <param name="settings">Collection to parse.</param>
  116. /// <param name="result1">Included addresses from the collection.</param>
  117. /// <param name="result2">Included IP4 addresses from the collection.</param>
  118. /// <param name="result3">Excluded addresses from the collection.</param>
  119. /// <param name="result4">Excluded IP4 addresses from the collection.</param>
  120. /// <param name="result5">Network addresses of the collection.</param>
  121. [Theory]
  122. [InlineData(
  123. "127.0.0.1#",
  124. "[]",
  125. "[]",
  126. "[]",
  127. "[]",
  128. "[]")]
  129. [InlineData(
  130. "!127.0.0.1",
  131. "[]",
  132. "[]",
  133. "[127.0.0.1/32]",
  134. "[127.0.0.1/32]",
  135. "[]")]
  136. [InlineData(
  137. "",
  138. "[]",
  139. "[]",
  140. "[]",
  141. "[]",
  142. "[]")]
  143. [InlineData(
  144. "192.158.1.2/16, localhost, fd23:184f:2029:0:3139:7386:67d7:d517, !10.10.10.10",
  145. "[192.158.1.2/16,[127.0.0.1/32,::1/128],fd23:184f:2029:0:3139:7386:67d7:d517/128]",
  146. "[192.158.1.2/16,127.0.0.1/32]",
  147. "[10.10.10.10/32]",
  148. "[10.10.10.10/32]",
  149. "[192.158.0.0/16,127.0.0.1/32,::1/128,fd23:184f:2029:0:3139:7386:67d7:d517/128]")]
  150. [InlineData(
  151. "192.158.1.2/255.255.0.0,192.169.1.2/8",
  152. "[192.158.1.2/16,192.169.1.2/8]",
  153. "[192.158.1.2/16,192.169.1.2/8]",
  154. "[]",
  155. "[]",
  156. "[192.158.0.0/16,192.0.0.0/8]")]
  157. public void TestCollections(string settings, string result1, string result2, string result3, string result4, string result5)
  158. {
  159. if (settings == null)
  160. {
  161. throw new ArgumentNullException(nameof(settings));
  162. }
  163. var conf = new NetworkConfiguration()
  164. {
  165. EnableIPV6 = true,
  166. EnableIPV4 = true,
  167. };
  168. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  169. // Test included.
  170. Collection<IPObject> nc = nm.CreateIPCollection(settings.Split(','), false);
  171. Assert.Equal(nc.AsString(), result1);
  172. // Test excluded.
  173. nc = nm.CreateIPCollection(settings.Split(','), true);
  174. Assert.Equal(nc.AsString(), result3);
  175. conf.EnableIPV6 = false;
  176. nm.UpdateSettings(conf);
  177. // Test IP4 included.
  178. nc = nm.CreateIPCollection(settings.Split(','), false);
  179. Assert.Equal(nc.AsString(), result2);
  180. // Test IP4 excluded.
  181. nc = nm.CreateIPCollection(settings.Split(','), true);
  182. Assert.Equal(nc.AsString(), result4);
  183. conf.EnableIPV6 = true;
  184. nm.UpdateSettings(conf);
  185. // Test network addresses of collection.
  186. nc = nm.CreateIPCollection(settings.Split(','), false);
  187. nc = nc.AsNetworks();
  188. Assert.Equal(nc.AsString(), result5);
  189. }
  190. /// <summary>
  191. /// Union two collections.
  192. /// </summary>
  193. /// <param name="settings">Source.</param>
  194. /// <param name="compare">Destination.</param>
  195. /// <param name="result">Result.</param>
  196. [Theory]
  197. [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]")]
  198. [InlineData("127.0.0.1", "127.0.0.1/8", "[127.0.0.1/32]")]
  199. public void UnionCheck(string settings, string compare, string result)
  200. {
  201. if (settings == null)
  202. {
  203. throw new ArgumentNullException(nameof(settings));
  204. }
  205. if (compare == null)
  206. {
  207. throw new ArgumentNullException(nameof(compare));
  208. }
  209. if (result == null)
  210. {
  211. throw new ArgumentNullException(nameof(result));
  212. }
  213. var conf = new NetworkConfiguration()
  214. {
  215. EnableIPV6 = true,
  216. EnableIPV4 = true,
  217. };
  218. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  219. Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(','), false);
  220. Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(','), false);
  221. Assert.Equal(nc1.ThatAreContainedInNetworks(nc2).AsString(), result);
  222. }
  223. [Theory]
  224. [InlineData("192.168.5.85/24", "192.168.5.1")]
  225. [InlineData("192.168.5.85/24", "192.168.5.254")]
  226. [InlineData("10.128.240.50/30", "10.128.240.48")]
  227. [InlineData("10.128.240.50/30", "10.128.240.49")]
  228. [InlineData("10.128.240.50/30", "10.128.240.50")]
  229. [InlineData("10.128.240.50/30", "10.128.240.51")]
  230. [InlineData("127.0.0.1/8", "127.0.0.1")]
  231. public void IpV4SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  232. {
  233. var ipAddressObj = IPNetAddress.Parse(netMask);
  234. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  235. }
  236. [Theory]
  237. [InlineData("192.168.5.85/24", "192.168.4.254")]
  238. [InlineData("192.168.5.85/24", "191.168.5.254")]
  239. [InlineData("10.128.240.50/30", "10.128.240.47")]
  240. [InlineData("10.128.240.50/30", "10.128.240.52")]
  241. [InlineData("10.128.240.50/30", "10.128.239.50")]
  242. [InlineData("10.128.240.50/30", "10.127.240.51")]
  243. public void IpV4SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  244. {
  245. var ipAddressObj = IPNetAddress.Parse(netMask);
  246. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  247. }
  248. [Theory]
  249. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  250. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
  251. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
  252. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
  253. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  254. public void IpV6SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
  255. {
  256. var ipAddressObj = IPNetAddress.Parse(netMask);
  257. Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  258. }
  259. [Theory]
  260. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
  261. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
  262. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
  263. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
  264. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
  265. public void IpV6SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
  266. {
  267. var ipAddressObj = IPNetAddress.Parse(netMask);
  268. Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
  269. }
  270. [Theory]
  271. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1/32")]
  272. [InlineData("10.0.0.0/8", "10.10.10.1/32")]
  273. [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1")]
  274. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1/32")]
  275. [InlineData("10.10.0.0/16", "10.10.10.1/32")]
  276. [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1")]
  277. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1/32")]
  278. [InlineData("10.10.10.0/24", "10.10.10.1/32")]
  279. [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1")]
  280. public void TestSubnetContains(string network, string ip)
  281. {
  282. Assert.True(IPNetAddress.TryParse(network, out var networkObj));
  283. Assert.True(IPNetAddress.TryParse(ip, out var ipObj));
  284. Assert.True(networkObj.Contains(ipObj));
  285. }
  286. [Theory]
  287. [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")]
  288. [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")]
  289. [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")]
  290. [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")]
  291. [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", "")]
  292. public void TestCollectionEquality(string source, string dest, string result)
  293. {
  294. if (source == null)
  295. {
  296. throw new ArgumentNullException(nameof(source));
  297. }
  298. if (dest == null)
  299. {
  300. throw new ArgumentNullException(nameof(dest));
  301. }
  302. if (result == null)
  303. {
  304. throw new ArgumentNullException(nameof(result));
  305. }
  306. var conf = new NetworkConfiguration()
  307. {
  308. EnableIPV6 = true,
  309. EnableIPV4 = true
  310. };
  311. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  312. // Test included, IP6.
  313. Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(','));
  314. Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(','));
  315. Collection<IPObject> ncResult = ncSource.ThatAreContainedInNetworks(ncDest);
  316. Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(','));
  317. Assert.True(ncResult.Compare(resultCollection));
  318. }
  319. [Theory]
  320. [InlineData("10.1.1.1/32", "10.1.1.1")]
  321. [InlineData("192.168.1.254/32", "192.168.1.254/255.255.255.255")]
  322. public void TestEquals(string source, string dest)
  323. {
  324. Assert.True(IPNetAddress.Parse(source).Equals(IPNetAddress.Parse(dest)));
  325. Assert.True(IPNetAddress.Parse(dest).Equals(IPNetAddress.Parse(source)));
  326. }
  327. [Theory]
  328. // Testing bind interfaces.
  329. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  330. //
  331. // This test is to replicate how DNLA requests work throughout the system.
  332. // User on internal network, we're bound internal and external - so result is internal.
  333. [InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
  334. // User on external network, we're bound internal and external - so result is external.
  335. [InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
  336. // User on internal network, we're bound internal only - so result is internal.
  337. [InlineData("10.10.10.10", "eth16", false, "eth16")]
  338. // User on internal network, no binding specified - so result is the 1st internal.
  339. [InlineData("192.168.1.1", "", false, "eth16")]
  340. // User on external network, internal binding only - so result is the 1st internal.
  341. [InlineData("jellyfin.org", "eth16", false, "eth16")]
  342. // User on external network, no binding - so result is the 1st external.
  343. [InlineData("jellyfin.org", "", false, "eth11")]
  344. // User assumed to be internal, no binding - so result is the 1st internal.
  345. [InlineData("", "", false, "eth16")]
  346. public void TestBindInterfaces(string source, string bindAddresses, bool ipv6enabled, string result)
  347. {
  348. if (source == null)
  349. {
  350. throw new ArgumentNullException(nameof(source));
  351. }
  352. if (bindAddresses == null)
  353. {
  354. throw new ArgumentNullException(nameof(bindAddresses));
  355. }
  356. if (result == null)
  357. {
  358. throw new ArgumentNullException(nameof(result));
  359. }
  360. var conf = new NetworkConfiguration()
  361. {
  362. LocalNetworkAddresses = bindAddresses.Split(','),
  363. EnableIPV6 = ipv6enabled,
  364. EnableIPV4 = true
  365. };
  366. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  367. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  368. NetworkManager.MockNetworkSettings = string.Empty;
  369. _ = nm.TryParseInterface(result, out Collection<IPObject>? resultObj);
  370. if (resultObj != null)
  371. {
  372. result = ((IPNetAddress)resultObj[0]).ToString(true);
  373. var intf = nm.GetBindInterface(source, out int? _);
  374. Assert.Equal(intf, result);
  375. }
  376. }
  377. [Theory]
  378. // Testing bind interfaces. These are set for my system so won't work elsewhere.
  379. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  380. //
  381. // This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
  382. // User on internal network, we're bound internal and external - so result is internal override.
  383. [InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
  384. // User on external network, we're bound internal and external - so result is override.
  385. [InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  386. // User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
  387. [InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
  388. // User on internal network, no binding specified - so result is the 1st internal.
  389. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  390. // User on external network, internal binding only - so assumption is a proxy forward, return external override.
  391. [InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
  392. // User on external network, no binding - so result is the 1st external which is overriden.
  393. [InlineData("jellyfin.org", "192.168.1.0/24", "", false, "0.0.0.0 = http://helloworld.com", "http://helloworld.com")]
  394. // User assumed to be internal, no binding - so result is the 1st internal.
  395. [InlineData("", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
  396. // User is internal, no binding - so result is the 1st internal, which is then overridden.
  397. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
  398. public void TestBindInterfaceOverrides(string source, string lan, string bindAddresses, bool ipv6enabled, string publishedServers, string result)
  399. {
  400. if (lan == null)
  401. {
  402. throw new ArgumentNullException(nameof(lan));
  403. }
  404. if (bindAddresses == null)
  405. {
  406. throw new ArgumentNullException(nameof(bindAddresses));
  407. }
  408. var conf = new NetworkConfiguration()
  409. {
  410. LocalNetworkSubnets = lan.Split(','),
  411. LocalNetworkAddresses = bindAddresses.Split(','),
  412. EnableIPV6 = ipv6enabled,
  413. EnableIPV4 = true,
  414. PublishedServerUriBySubnet = new string[] { publishedServers }
  415. };
  416. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  417. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  418. NetworkManager.MockNetworkSettings = string.Empty;
  419. if (nm.TryParseInterface(result, out Collection<IPObject>? resultObj) && resultObj != null)
  420. {
  421. // Parse out IPAddresses so we can do a string comparison. (Ignore subnet masks).
  422. result = ((IPNetAddress)resultObj[0]).ToString(true);
  423. }
  424. var intf = nm.GetBindInterface(source, out int? _);
  425. Assert.Equal(intf, result);
  426. }
  427. [Theory]
  428. [InlineData("185.10.10.10,200.200.200.200", "79.2.3.4", true)]
  429. [InlineData("185.10.10.10", "185.10.10.10", false)]
  430. [InlineData("", "100.100.100.100", false)]
  431. public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIpsInWhitelist(string addresses, string remoteIp, bool denied)
  432. {
  433. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  434. // If left blank, all remote addresses will be allowed.
  435. var conf = new NetworkConfiguration()
  436. {
  437. EnableIPV4 = true,
  438. RemoteIPFilter = addresses.Split(','),
  439. IsRemoteIPFilterBlacklist = false
  440. };
  441. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  442. Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
  443. }
  444. [Theory]
  445. [InlineData("185.10.10.10", "79.2.3.4", false)]
  446. [InlineData("185.10.10.10", "185.10.10.10", true)]
  447. [InlineData("", "100.100.100.100", false)]
  448. public void HasRemoteAccess_GivenBlacklist_BlacklistTheIps(string addresses, string remoteIp, bool denied)
  449. {
  450. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  451. // If left blank, all remote addresses will be allowed.
  452. var conf = new NetworkConfiguration()
  453. {
  454. EnableIPV4 = true,
  455. RemoteIPFilter = addresses.Split(','),
  456. IsRemoteIPFilterBlacklist = true
  457. };
  458. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  459. Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
  460. }
  461. [Theory]
  462. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.209")] // Only 1 address so use it.
  463. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.208")] // LAN address is specified by default.
  464. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "10.0.0.1")] // return bind address
  465. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "192.168.1.209")] // return internal bind address (Internal takes priority)
  466. public void Get_Appropriate_Interface_NoSource(string interfaces, string lan, string bind, string result)
  467. {
  468. var conf = new NetworkConfiguration()
  469. {
  470. EnableIPV4 = true,
  471. LocalNetworkSubnets = lan.Split(','),
  472. LocalNetworkAddresses = bind.Split(',')
  473. };
  474. NetworkManager.MockNetworkSettings = interfaces;
  475. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  476. var interfaceToUse = nm.GetBindInterface(string.Empty, out var port);
  477. Assert.Equal(interfaceToUse, result);
  478. }
  479. [Theory]
  480. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.210", "192.168.1.209")] // Source on LAN
  481. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.209", "192.168.1.208")] // Source on LAN
  482. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "8.8.8.8", "10.0.0.1")] // Source external.
  483. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "192.168.1.209", "10.0.0.1")] // LAN not bound, so return external.
  484. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "8.8.8.8", "10.0.0.1")] // return external bind address
  485. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "192.168.1.210", "192.168.1.208")] // return LAN bind address
  486. public void Get_Appropriate_Interface_ForSource(string interfaces, string lan, string bind, string source, string result)
  487. {
  488. var conf = new NetworkConfiguration()
  489. {
  490. EnableIPV4 = true,
  491. LocalNetworkSubnets = lan.Split(','),
  492. LocalNetworkAddresses = bind.Split(',')
  493. };
  494. NetworkManager.MockNetworkSettings = interfaces;
  495. using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
  496. var interfaceToUse = nm.GetBindInterface(source, out var port);
  497. Assert.Equal(interfaceToUse, result);
  498. }
  499. }
  500. }