NetworkParseTests.cs 24 KB

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