NetworkParseTests.cs 23 KB

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