NetworkParseTests.cs 24 KB

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